zoukankan      html  css  js  c++  java
  • JZYZOJ1452 NOIP2015_提高组Day2_1_跳石头

    http://172.20.6.3/Problem_Show.asp?id=1452
    很简单的二分,最开始以为是优先队列,想了想发现优先队列是有情况不能达到最优的,所以二分+贪心处理,在贪心check的时候想得有点复杂(或者说有漏洞),调了几次才过。
    写代码的时候果然不能硬莽,不思考乱莽只能随机30,70,90分,没法ac。

    代码

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<cmath>
     5 #include<iostream>
     6 using namespace std;
     7 const int maxn=100010;
     8 int z,m,n;
     9 int a[maxn]={},tot=0;
    10 int check(int x){
    11     int y=0;int w=0,hug=z,t1=z;
    12     for(int i=1;i<=n+1;i++){
    13         if(a[i]-a[y]>=x){
    14             hug=min(hug,t1);
    15             t1=a[i]-a[y];
    16             w+=(i-y-1);y=i;
    17         }
    18     }if(n+1!=y){
    19         w+=n+1-y;
    20         if(y==0)w--;
    21         t1+=a[n+1]-a[y];
    22         hug=min(hug,t1);
    23     }
    24     if(w==m){
    25         if(hug<x) return 0;
    26         else return 1;
    27     }
    28     else if(w>m) return 0;
    29     else return 1;
    30 }
    31 int doit(){
    32     int l=0,r=z;
    33     while(l+1<r){
    34         int mid=(l+r)/2;
    35         int w=check(mid);
    36         if(!w)r=mid-1;
    37         else l=mid;
    38     }int w=check(r);
    39     if(!w)return l;
    40     else return r;
    41 }
    42 int main(){
    43     //freopen("wtf.in","r",stdin);
    44     scanf("%d%d%d",&z,&n,&m);
    45     tot=n+1;
    46     for(int i=1;i<=n;i++){
    47         scanf("%d",&a[i]);
    48     }a[n+1]=z;
    49     printf("%d
    ",doit());
    50     return 0;
    51 }
    View Code
  • 相关阅读:
    tricky c++ new(this)
    MCI使用
    LoadIcon的使用
    深入浅出Node.js (2)
    洛谷 P1464 Function
    洛谷 P1722 矩阵 II
    洛谷 P1036 选数
    洛谷 P1303 A*B Problem
    洛谷 P2694 接金币
    洛谷 P1679 神奇的四次方数
  • 原文地址:https://www.cnblogs.com/137shoebills/p/7788265.html
Copyright © 2011-2022 走看看