【POJ】3258 River Hopscotch 不仅仅是二分
2016-12-21 15:11
309 查看
River Hopscotch
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 12303 Accepted: 5271
Description
Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. The excitement takes place on a long, straight river with a rock at the start and another rock at the end, L units away from the start (1 ≤ L ≤ 1,000,000,000). Along the river between the starting and ending rocks, N (0 ≤ N ≤ 50,000) more rocks appear, each at an integral distance Di from the start (0 < Di < L).
To play the game, each cow in turn starts at the starting rock and tries to reach the finish at the ending rock, jumping only from rock to rock. Of course, less agile cows never make it to the final rock, ending up instead in the river.
Farmer John is proud of his cows and watches this event each year. But as time goes by, he tires of watching the timid cows of the other farmers limp across the short distances between rocks placed too closely together. He plans to remove several rocks in order to increase the shortest distance a cow will have to jump to reach the end. He knows he cannot remove the starting and ending rocks, but he calculates that he has enough resources to remove up to M rocks (0 ≤ M ≤ N).
FJ wants to know exactly how much he can increase the shortest distance before he starts removing the rocks. Help Farmer John determine the greatest possible shortest distance a cow has to jump after removing the optimal set of M rocks.
Input
Line 1: Three space-separated integers: L, N, and M
Lines 2..N+1: Each line contains a single integer indicating how far some rock is away from the starting rock. No two rocks share the same position.
Output
Line 1: A single integer that is the maximum of the shortest distance a cow has to jump after removing M rocks
Sample Input
25 5 2
2
14
11
21
17
Sample Output
4
Hint
Before removing any rocks, the shortest jump was a jump of 2 from 0 (the start) to 2. After removing the rocks at 2 and 14, the shortest required jump is a jump of 4 (from 17 to 21 or from 21 to 25).
题意大
4000
致是除M颗石头让最小间距的值最大
我的代码
附上学长的代码留作参考
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 12303 Accepted: 5271
Description
Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. The excitement takes place on a long, straight river with a rock at the start and another rock at the end, L units away from the start (1 ≤ L ≤ 1,000,000,000). Along the river between the starting and ending rocks, N (0 ≤ N ≤ 50,000) more rocks appear, each at an integral distance Di from the start (0 < Di < L).
To play the game, each cow in turn starts at the starting rock and tries to reach the finish at the ending rock, jumping only from rock to rock. Of course, less agile cows never make it to the final rock, ending up instead in the river.
Farmer John is proud of his cows and watches this event each year. But as time goes by, he tires of watching the timid cows of the other farmers limp across the short distances between rocks placed too closely together. He plans to remove several rocks in order to increase the shortest distance a cow will have to jump to reach the end. He knows he cannot remove the starting and ending rocks, but he calculates that he has enough resources to remove up to M rocks (0 ≤ M ≤ N).
FJ wants to know exactly how much he can increase the shortest distance before he starts removing the rocks. Help Farmer John determine the greatest possible shortest distance a cow has to jump after removing the optimal set of M rocks.
Input
Line 1: Three space-separated integers: L, N, and M
Lines 2..N+1: Each line contains a single integer indicating how far some rock is away from the starting rock. No two rocks share the same position.
Output
Line 1: A single integer that is the maximum of the shortest distance a cow has to jump after removing M rocks
Sample Input
25 5 2
2
14
11
21
17
Sample Output
4
Hint
Before removing any rocks, the shortest jump was a jump of 2 from 0 (the start) to 2. After removing the rocks at 2 and 14, the shortest required jump is a jump of 4 (from 17 to 21 or from 21 to 25).
题意大
4000
致是除M颗石头让最小间距的值最大
我的代码
#include<cstdio> #include<algorithm> using namespace std; int L,N,M;int a[5000000+5]; int check(int ans) { int last=0,now,m=0; while(last<N-1) { if(ans+a[last]<=L) now=lower_bound(a,a+N,ans+a[last])-a; else now=N; m+=now-last-1; last=now; } return m>M;//注意 m==M 时 ans不一定是答案保留low=ans m>M时ans一定不是最终答案保留top=ans } int main() { while(~scanf("%d%d%d",&L,&N,&M)) { for(int i=0;i<N;i++) scanf("%d",&a[i]); if(N==M) { printf("%d\n",L); continue; } a[N++]=0,a[N++]=L; sort(a,a+N); int low=0,top=L,ans; while(top-low>1)//目的时找到使chech成立的最大答案 { int m=(top+low)>>1; int k=check(m); if(k) top=m; else low=m; } printf("%d\n",low);//low 是一定满足条件的答案 top是一定不满足条件的答案 满足条件的最大值就是low } return 0; }
附上学长的代码留作参考
#include <cstdio> #include <algorithm> using namespace std; int w,n,k; int num[50000+11]; bool check(int x) { int pos,t; int rec = k; pos = lower_bound (num , num + n , x) - num; rec = rec - pos; if (rec < 0) return false; else if (pos == n) return true; while (rec >= 0) { t = lower_bound (num , num + n , num[pos] + x) - num; rec = rec - (t - pos - 1); if (rec < 0) return false; else if (t == n) return true; pos = t; } } int main() { int l,r,mid; while (~scanf ("%d %d %d",&w,&n,&k)) { num[0] = w; for (int i = 1 ; i <= n ; i++) scanf ("%d",&num[i]); n++; sort (num , num + n); l = 1; r = num[n-1]; while (r >= l) { mid = (l + r) >> 1; if (check (mid)) l = mid + 1; else r = mid - 1; } // printf ("%d\n",l); //这道题的情况特殊,结果并不是l printf ("%d\n",l-1); } return 0; }
相关文章推荐
- POJ 3258 River Hopscotch 二分答案
- POJ - 3258 River Hopscotch 二分
- POJ 3258 River Hopscotch【二分搜索】
- POJ 3258:River Hopscotch 二分的好想法
- POJ 3258 River Hopscotch 二分查找
- River Hopscotch POJ - 3258 (二分)
- River Hopscotch POJ - 3258 (二分求最小值)
- Poj3258 River Hopscotch 二分
- River Hopscotch POJ - 3258 (二分求最小值)
- poj-3258-River Hopscotch-二分
- POJ_3258_River Hopscotch_二分搜索
- POJ-3258 River Hopscotch 二分查找+贪心
- River Hopscotch POJ - 3258 (二分求最小值)
- POJ 3258 River Hopscotch 二分答案
- River Hopscotch POJ - 3258 (二分求最小值)
- POJ 3258 River Hopscotch 二分
- POJ 3258 River Hopscotch <二分>
- POJ 3258 River Hopscotch 【二分 + 思维】
- River Hopscotch POJ - 3258 (二分求最小值)
- POJ - 3258/USACO - Dec06 Silver/NOIP 2015 - 提高组 River Hopscotch 贪心+二分搜索