洛谷 P2678 跳石头 题解

输入输出样例

25 5 2
2
11
14
17
21

4

说明

AC代码：

 1 #include<cstdio>
2 #include<algorithm>
3 #include<cmath>
4 #include<cstring>
5 #include<stack>
6
7 const int MAXN = 500005;
8 inline void read(long long &x)
9 {
10     char ch = getchar(),c = ch;x = 0;
11     while(ch < '0' || ch > '9') c = ch,ch = getchar();
12     while(ch <= '9' && ch >= '0') x = (x<<1)+(x<<3)+ch-'0',ch = getchar();
13     if(c == '-') x = -x;
14 }
15
16 long long L,n,m,l,r,mid,ans,dis[MAXN];
17
18 bool jud(long long x)
19 {
20     long long cnt = 0,last = 0;
21     for(int i = 1;i <= n;++ i)
22     {
23         if(dis[i]-last < x)
24             cnt ++;
25         else
26             last = dis[i];
27     }
28     if(cnt <= m) return true;
29     else return false;
30 }
31
32 int main()
33 {
35     for(int i = 1;i <= n;++ i)
37     if(dis[n] < L) dis[++n] = L;
38     l = 0,r = L+1;
39     while(l <= r)
40     {
41         mid = (l+r)>>1;
42         if(jud(mid)) ans = mid,l = mid+1;
43         else r = mid-1;
44     }
45     printf("%lld\n",ans);
46     return 0;
47 }

posted @ 2017-10-26 17:28 超时空灰狼菌 阅读(...) 评论(...) 编辑 收藏