hdu4004(二分法)

来源:互联网 发布:陕西软件行业天网 编辑:程序博客网 时间:2024/06/05 04:20

The Frog's Games

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


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
 


本题要求不超过跳跃次数的情况下使跳跃长度的最大值最小,而石子之多基本打消其他做法的念头。

枚举每次跳跃的最大长度,注意二分法的运用,优化过程。将石子加上两个端点构成一条线段;枚举极限长度使其不小于两个端点的长度(端点可以任意组合),使其总的跳跃次数不超过m且极限长度尽可能的小。注意边界条件的运用。

#include<iostream>#include<cstdio>#include<algorithm>using namespace std;const int MAXN=500000+100;int n,m,l;int stone[MAXN];bool Judge(__int64 mid){int i,cur=0,cnt=0;for(i=0;i<=n;){if(++cnt>m||stone[i]-cur>mid)return false;while(i<=n&&stone[i]-cur<=mid)i++;cur=stone[i-1];}return true;}int main(){int i;while(~scanf("%d%d%d",&l,&n,&m)){for(i=0;i<n;i++){scanf("%d",&stone[i]);}stone[i]=l;sort(stone,stone+n+1);int mid,low=0,high=l;while(low<=high){__int64 mid=(low+high)/2;if(Judge(mid))high=mid-1;elselow=mid+1;}printf("%d\n",low);}return 0;}


 

原创粉丝点击