River Hopscotch(POJ--3258【二分查找】
来源:互联网 发布:qq软件下载最新版 编辑:程序博客网 时间:2024/05/29 19:44
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 distanceDi 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
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
Sample Input
25 5 2214112117
Sample Output
4
Hint
#include <cstdio>#include <algorithm>using namespace std;int dis[50005],n;int re(int x){ int cnt=0; for(int i=1,j=0; i<=n+1; i++) { if((dis[i]-dis[j])<=x) //如果牛能从j跳到i就加加 cnt++; //累加牛能跳到的柱子数 else j=i; } return cnt;}int main(){ //freopen("lalala.text","r",stdin); int L,m; while(~scanf("%d %d %d",&L,&n,&m)) { dis[0]=0; for(int i=1; i<=n; i++) scanf("%d",&dis[i]); sort(dis+1,dis+n+1); //将柱子距离按升序排序 dis[n+1]=L; int l=0,r=L*2; //上限是总长的2倍 int mid; while(l<=r) { mid=(l+r)/2;// printf("l = %d r = %d\nmid = %d\n",l,r,mid); int t=re(mid); //牛能跳到的柱子数// printf("t = %d\n",t); if(t>m) //如果牛能跳到的柱子数比要被撤走的柱子数都多说明此时的距离大了 r=mid-1; else //如果牛能跳到的柱子数比要被撤走的柱子数少或相等(如果相等的话说明终点柱子也应该被撤走而终点柱子不能移动,所以也不符合要求)则说明此时的距离小了 l=mid+1; } printf("%d\n",l); } return 0;}
- River Hopscotch(POJ--3258【二分查找】
- poj 3258 River Hopscotch 二分查找
- [POJ](3258)River Hopscotch ---二分查找+贪心(查找+贪心)
- POJ 3258 River Hopscotch 二分
- POJ 3258 River Hopscotch 二分
- poj-3258-River Hopscotch-二分
- River Hopscotch - POJ 3258 二分
- 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
- N皇后
- 责任链模式
- 通过颜色创建image图片
- 虚拟化的最后一公里–虚拟化网卡
- 设计模式——组合模式
- River Hopscotch(POJ--3258【二分查找】
- 字符串
- iOS开发UI篇—程序启动原理和UIApplication
- eMMC DDR模式切换
- opengl介绍
- 输入三个整数,将他们排序然后输出中间那个数
- 入栈出栈序列问题
- MFC_CString 转换成TCHAR *的方法
- 变量的作用域