poj 3258 River Hopscotch
来源:互联网 发布:考试专家软件怎么样 编辑:程序博客网 时间:2024/06/01 09:56
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).
大意:有一条河长度L,中间放N个石头,现在要去掉其中M个,求剩下的石头间最小距离的最大值。
思路。二分,上界为河长L,下界0,对于每个mid,判断当前mid作为最短距离,一共可以去掉多少个石头。如果去掉的石头少于或等于M个,则说明可行,尝试更大的mid,否则尝试更小。
注意的问题,在判断可以去掉多少石头时,用cnt计数,last记录最后一块需要留下的石头。从1开始遍历每一块石头,last初始为0,如果当前的石头a[i]距离a[last]的距离不足或等于mid,cnt+1,继续判断下一块石头。如果超过了mid,则当前石头作为last,即把当前的i赋给last,继续循环判断。
代码
#include<stdio.h>#include<math.h>#include<algorithm>using namespace std;int l,n,m,a[50009];bool fun(int mid){ int cnt=0,last=0; for(int i=1;i<=n+1;i++) { if(a[i]-a[last]<=mid) cnt++; else last=i; } if(cnt<=m) return true; else return false;}int main(){ while(~scanf("%d%d%d",&l,&n,&m)) { a[0]=0; for(int i=1;i<=n;i++) scanf("%d",&a[i]); a[n+1]=l; sort(a+1,a+n+1); int le=0,ri=l,mid; while(ri>=le) { mid=(ri+le)/2; if(fun(mid)) le=mid+1; else ri=mid-1; } printf("%d\n",le); } return 0;}
- poj 3258 River Hopscotch
- poj-3258 River Hopscotch
- poj-3258 River Hopscotch
- poj 3258 River Hopscotch
- poj 3258 River Hopscotch
- POJ 3258 River Hopscotch
- POJ 3258 River Hopscotch
- poj 3258 River Hopscotch
- POJ-3258-River Hopscotch
- POJ 3258 River Hopscotch
- POJ 3258 River Hopscotch
- POJ 3258 River Hopscotch
- POJ 3258 River Hopscotch
- poj 3258-River Hopscotch
- POJ 3258 River Hopscotch
- POJ 3258 River Hopscotch
- POJ 3258--River Hopscotch
- poj 3258 River Hopscotch
- 牛顿法与拟牛顿法
- [LeetCode] 分治之 Median of Two Sorted Arrays 课后题算法实现 Hard
- VR设备的链接与使用(脚本VRhouse)
- 算法(二):树,以及变形
- POJ3041(二分图匹配)
- poj 3258 River Hopscotch
- Linux下unzip命令出现 command not found的解决办法
- Ubuntu下用vi打开文本的^M问题
- 密码强度的正则验证
- Graph Theory NO.7_HDU_1233_还是畅通工程_最小生成树
- ccf 公共钥匙盒 模拟
- 63.Scala中隐式类代码实战详解
- Slam设计模式
- 输入一个数 求0到这个数的阶乘的和 使用 do...while 循环