【HDU】-4004-The Frog's Games(二分,难)
2016-07-26 15:47
399 查看
The Frog's Games
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 6058 Accepted Submission(s): 2932
Problem Description
The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog Triathlon. One test in the Ironfrog Triathlon is jumping. This project requires the frog athletes to jump over the river. The width of the river is L (1<= L <= 1000000000).
There are n (0<= n <= 500000) stones lined up in a straight line from one side to the other side of the river. The frogs can only jump through the river, but they can land on the stones. If they fall into the river, they
are out. The frogs was asked to jump at most m (1<= m <= n+1) times. Now the frogs want to know if they want to jump across the river, at least what ability should they have. (That is the frog's longest jump distance).
Input
The input contains several cases. The first line of each case contains three positive integer L, n, and m.
Then n lines follow. Each stands for the distance from the starting banks to the nth stone, two stone appear in one place is impossible.
Output
For each case, output a integer standing for the frog's ability at least they should have.
Sample Input
6 1 2
2
25 3 3
11
2
18
Sample Output
4
11
宇神来讲的题,思路清晰极了,代码也是那么超然不凡(因为开始没理解(核心看不懂啊!!!),受到了伤害,~~~~(>_<)~~~~ ),不太好想。
对可跳的距离二分,判断是否可以跳到和次数是否满足要求
#include<cstdio> #include<cmath> #include<algorithm> using namespace std; int a[100000]; int L,n,m; bool dis(int x) { int cnt=0; //跳的次数 int last=0; //上一次的位置 for(int i=1;i<=n+1;) { if(a[i]<=last+x) //a[i]<=last+x可跳到a[i]寻找下一个 { i++; //比可跳的近,继续找下一个,直到下一个大于可跳的位置 } else { if(last==a[i-1]) //注意:如果last不是更新过来的而是不可以跳到下一个就还在原位,此时就不可能 return false; last=a[i-1]; //可以跳到的最远就是a[i-1],last更新 cnt++; //又跳一次 ,记录加1 } } cnt++; //最后总是停在a 上,到a 还需要一次 return cnt<=m; //跳的次数要小于等于给定的 m } int main() { while(~scanf("%d %d %d",&L,&n,&m)) { for(int i=1;i<=n;i++) scanf("%d",&a[i]); sort(a+1,a+1+n); a[n+1]=L; //最远就是L的长度,定义为a[n+1] int l=0,r=L; int ans; while(r>=l) { int mid=(l+r)>>1; if(dis(mid)) { ans=mid; //如果为真就是可满足的,记下此时满足的位置 ans r=mid-1; //那么每次如果跳的更远一定成立,就向前找,找小的 } else l=mid+1; } printf("%d\n",ans); } return 0; }
相关文章推荐
- 移动前端笔记
- Delphi 使窗体Showmodal后可以操作其他窗体
- python升级后带来的几个小问题
- redis下载地址
- display 用法
- 两个栈模拟一个队列 两个队列模似一个栈
- 问题二 如何导出Excel工作薄
- SQL SERVER 2005中同义词实例
- PyGobject(十六)布局容器之Button篇——Gtk.VolumeButton
- NYOJ
- 关于php的openssl 加密解密以及数字签名
- TCP连接的建立与终止,三次握手和四次握手,以及在socket系列函数中的体现
- Webstorm切换快捷键风格(Webstorm快捷键与eclipse对比介绍)
- sql server 2005 中的同义词
- C++多态性
- Fiddler On Linux
- haproxy 用命令踢掉和开启某个项目和主机
- U盘防拷精灵 v1.3
- Ubuntu Linux 安装 .7z 解压和压缩文件
- Ros By example-第八章 导航,路劲规划和SLAM