【luogu2678】【niop2015】跳石头 (二分)

   P2678跳石头 

我错了我以后一定自己多造几组数据再提交 样例都是骗人的

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define rg register
 4 const int N=50000+5,inf=1e9+7,mod=31011;
 5 int l,n,m,a[N],dis[N],mx,mn,cnt;
 6 bool use[N];
 7 inline int rd()
 8 {
 9     int x=0,w=0;char ch=0;
10     while(!isdigit(ch)) w|=ch=='-',ch=getchar();
11     while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
12     return w?-x:x;
13 }
14  
15 bool jud(int x)
16 {
17     for(int i=0;i<=n;i++)
18     {
19         int tot=1;
20         while(use[i+tot]) tot++;
21         while(a[i+tot]-a[i]<x)
22         {
23             tot++,cnt++;
24             use[i+tot]=1;
25             if(cnt>n) return 0;
26         }
27         i+=tot;
28     //  fh=min(fh,a[i+tot]-a[i]);
29     }
30     return 1;
31 }
32  
33 int main()
34 {
35     l=rd(),n=rd(),m=rd();
36     mn=inf,a[0]=mx=0;
37     for(rg int i=1;i<=n;i++) 
38     {
39         a[i]=rd();
40         mn=min(mn,a[i]-a[i-1]);mx=max(mx,a[i]-a[i-1]);
41     }
42     mx=max(l-a[n],mx);
43     int ans=0;
44     while(mn<=mx)
45     {
46         cnt=0;
47         int mid=(mx+mn)>>1;
48         memset(use,0,sizeof(use));
49         if(jud(mid)) mx=mid-1;
50         else mn=mid+1;
51     }
52     printf("%d",mn);
53     return 0;
54 }
10昏 漏洞百出
  • 打着打着就忘了是求最大的最小距离
  • 然后忘了设置跳的边界
  • 是输出左界-1 QAQ ans=mid

  都这样了我还能过样例QAQ!!!

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define rg register
 4 const int N=50000+5,inf=1e9+7,mod=31011;
 5 int l,n,m,a[N],dis[N],mx,mn,cnt;
 6 bool use[N];
 7 inline int rd()
 8 {
 9     int x=0,w=0;char ch=0;
10     while(!isdigit(ch)) w|=ch=='-',ch=getchar();
11     while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
12     return w?-x:x;
13 }
14  
15 bool jud(int x)
16 {
17     for(int i=0;i<=n;i++)
18     {
19         int tot=1;
20         while(use[i+tot]) ++tot;//如果被搬走了 那就到下一个石头
21         while(a[i+tot]-a[i]<x&&i+tot<=n+1)//不能跳过了
22         {
23             use[i+tot]=1;
24             tot++,cnt++;
25             if(cnt>m) return 0;
26         }
27         i+=(tot-1);//因为一次循环结束后它会+1 所得-1
28     }
29     return 1;
30 }
31  
32 int main()
33 {
34     l=rd(),n=rd(),m=rd();
35     mn=inf,a[0]=mx=0,a[n+1]=l;
36     for(rg int i=1;i<=n;i++) 
37     {
38         a[i]=rd();
39     //  mn=min(mn,a[i]-a[i-1]);
40     //  mx=max(mx,a[i]-a[i-1]);
41     }
42 //  mx=max(l-a[n],mx);
43     mx=l,mn=0;
44     while(mn<=mx)
45     {
46         int mid=(mx+mn)>>1;
47         memset(use,0,sizeof(use));
48         cnt=0;
49         if(jud(mid)) mn=mid+1;
50         else mx=mid-1;
51     }
52     printf("%d",mn-1);
53     return 0;
54 }
100昏 二分

猜你喜欢

转载自www.cnblogs.com/lxyyyy/p/10410332.html
今日推荐