【HDU】-4004-The Frog's Games(二分,难)
来源:互联网 发布:淘宝买家恶意拍下付款 编辑:程序博客网 时间:2024/05/29 18:46
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).
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.
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 2225 3 311 218
Sample Output
411
对可跳的距离二分,判断是否可以跳到和次数是否满足要求
#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[n]上,到a[n]还需要一次 returncnt<=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;//如果为真就是可满足的,记下此时满足的位置 ansr=mid-1;//那么每次如果跳的更远一定成立,就向前找,找小的 }elsel=mid+1;}printf("%d\n",ans);}return 0;}
0 0
- 【HDU】-4004-The Frog's Games(二分,难)
- HDU 4004 THE FROG’S GAMES(二分)
- HDU 4004 The Frog's Games(二分)
- HDU 4004 The Frog's Games(二分)
- hdu 4004 The Frog's Games【二分】
- hdu 4004 The Frog's Games 【二分】
- HDU 4004 The Frog's Games (二分)
- HDU 4004 The Frog's Games [二分]
- HDU-4004-The Frog's Games【二分】
- 【HDU 4004 The Frog's Games】+ 二分
- HDU:4004 The Frog's Games<二分>
- Hdu 4004 The Frog's Games(二分)
- HDU 4004 The Frog's Games(二分)
- hdu 4004 The Frog's Games ( 二分+贪心 )
- HDU 4004 The Frog‘s Games(二分)
- hdu 4004 The Frog's Games(二分最小化最大值)
- HDU 4004 The Frog's Games (二分贪心)
- Hdu 4004 The Frog's Games(二分)
- Oracle的sequence的重置
- python创建文件
- 欢迎使用CSDN-markdown编辑器
- 如何用Linux的命令正确识别cpu的个数和核数
- python 报错
- 【HDU】-4004-The Frog's Games(二分,难)
- 121. Best Time to Buy and Sell Stock
- JavaScript ---- 懒加载demo
- 【LightOJ】1137 - Expanding Rods(二分 & 几何)
- javaweb学习总结(六)——Servlet开发(二)
- eclipse 断点调试
- 【linux c】c语言关键字作用及注意事项_学习笔记_013
- 打造Android微信朋友圈下拉刷新控件
- UVA 489 Hangman Judge【刽子手】