The Frog's Games
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 8921 Accepted Submission(s): 4142
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<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int a[500010];
int l,n,m;
bool jump(int x){
int ans=1,pos=0;
for(int i=1;i<=n;i++){
if(a[i]-a[pos]<=x&&a[i+1]-a[pos]>x){
/*任意两块石头之间的距离都要比二分的距离要小,并且每隔一块石头的
距离要比二分要大*/
pos=i;
ans++;//记录跳的步数
}
}
if(ans>m) return false;
else return true;
}
int main(){
int mind,lower,higher,sum,mid;
while(~scanf("%d %d %d",&l,&n,&m)){
a[0]=0;//两个岸边分别到出发岸的距离
a[n+1]=l;
mind=0;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
sort(a,a+n+1);
//int mind=0;//青蛙跳的最小距离为两块石头之间的最大距离
for(int i=1;i<=n+1;i++){
mind=max(mind,a[i]-a[i-1]);
}
//int sum;
lower=0;
higher=l;
while(higher>=lower){
mid=(lower+higher)/2;
if(jump(mid)){
sum=mid;
higher=mid-1;//每一步都要是最终的答案向中间靠拢
}
else lower=mid+1;
}
printf("%d\n",sum);
}
return 0;
}