HDU 4004 The Frog's Games(二分)

来源:互联网 发布:开源数据建模工具 编辑:程序博客网 时间:2024/05/16 11:01

http://acm.hdu.edu.cn/showproblem.php?pid=4004



The Frog's Games

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 3072    Accepted Submission(s): 1520


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 2225 3 311 218
 

Sample Output
411
 

Source
The 36th ACM/ICPC Asia Regional Dalian Site —— Online Contest
 


青蛙过河,总共距离为L,河中有石,知道石头离起点的距离,青蛙跳m次到对岸,求青蛙最小的最大跳跃距离


二分,预处理出青蛙跳跃距离的上界和下界,对距离二分求出跳跃次数,具体见代码注释



#include<cstdio>#include<iostream>#include<cstdlib>#include<algorithm>#include<ctime>#include<cctype>#include<cmath>#include<string>#include<cstring>#include<queue>#include<vector>#define sqr(x) (x)*(x)#define INF 0x1f1f1f1f#define PI 3.1415926535#define LL long long#define mm 500005using namespace std;int stone[mm];int l,n,m,d,le,ri;int main(){while (~scanf("%d%d%d",&l,&n,&m)){le=0;for (int i=0;i<n;i++){scanf("%d",&stone[i]);}stone[n++]=l;//把河对岸当做最后一块石头 sort(stone,stone+n);int begin=0;for (int i=0;i<n;i++){le=max(le,stone[i]-begin);//下界是两块石头间的最大距离 begin=stone[i];}ri=l;//上界,跳一下就到对岸 while (le<ri){int mid=(le+ri)>>1;int cnt=1,begin=0;//cnt=1 如果初始化为0,最后一次跳跃就没有被计算,想想这是为什么 for (int i=0;i<n;i++){if (stone[i]-begin>mid)//跳不过去了 {begin=stone[i-1];cnt++;}if (cnt>m)//这里可以进行优化 {break;}}//printf("l:%d r:%d mid:%d cnt:%d\n",le,ri,mid,cnt);/*注意mid我用的是下取整 */ if (cnt>m){le=mid+1;}else{ri=mid;}}printf("%d\n",le);}return 0;}


1 0
原创粉丝点击