[hdu] The frog's Game
来源:互联网 发布:xp芯片编程器 编辑:程序博客网 时间:2024/05/31 06:22
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
题目大意:青蛙要过河,给出河的长度还有河上的石头的位置,以及青蛙最多能跳多少次。求青蛙【最强跳跃能力】的最小值。
思路:先将石头排序,从小到大,其中a[0]=0表示原点(注意sort中是从a到a+n+1)。然后用二分法,找到题目中要的最小值。
#include "cstdio"#include "cmath"#include "cstring"#include "algorithm"using namespace std;int a[500005];int L,n,m;int f(int x)//判断是否能跳过去 {int s=0,step=0;//s是每次跳跃的起点,step是已经跳跃的步数 if(x*m<L||x<L-a[n])return 0;//剪枝 for(int i=1;i<n+1;i++)//关键代码 找到从当前起点能跳到的最远的石头,并且i<n+1,因为底下有a[i+1] {if(x>=a[i]-a[s]&&x<a[i+1]-a[s])// 恰好这次能跳过去,下次不能跳过去,则这次是跳得最远的(贪心算法) {s=i;step++;}}step++;//跳到最后一块石头以后,还要从石头跳到岸上 if(step>m)return 0;else return 1;}int main(){while(scanf("%d %d %d",&L,&n,&m)!=EOF){memset(a,0,sizeof(a));for(int i=1;i<=n;i++){scanf("%d",&a[i]);}a[0]=0;sort(a,a+n+1);a[n+1]=L;int maxn=0;for(int i=1;i<=n+1;i++)//为了方便,找到两块石头距离的最大值 {if(maxn<a[i]-a[i-1])maxn=a[i]-a[i-1];}int r=L,l=maxn;while(r>=l){int mid=(r+l)/2;if(f(mid)==1)r=mid-1;else l=mid+1;}printf("%d\n",l);}return 0;}
0 0
- [hdu] The frog's Game
- 【HDU 1005 Frog’s Game】
- The Frog's Games HDU
- hdu 5926/ Mr. Frog’s Game
- HDU 5926 Mr. Frog’s Game
- 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(二分)
- The Frog’s Games(hdu 4004)
- 使用jquery给加载失败的img元素显示默认图片
- 光流法
- 黑马程序员---抽象类与抽象方法
- 如此保证选举公正性能成吗
- 单例模式的两种形式(恶汉式,懒汉式)
- [hdu] The frog's Game
- MySQL数据库引擎介绍、区别、创建和性能测试的深入分析
- JAVA链表中迭代器的实现
- ubuntu14.04LTS MATLAB2014b
- servlet类如何写
- 五个软件的软件说明图示
- android快速开发系列 10个常用工具类
- 关于storyboard的理解
- 将sublime 配置成python开发工具及如何更改IPython默认编辑器