The Frog's Games
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
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 2 2 25 3 3 11 2 18
Sample Output
4 11
先求出两相邻石头之间的距离的最大值MAX,MAX是青蛙在跳跃次数无限制的情况下的最小跳跃能力(如果这个都不满足,青蛙根本都过不了河),然后青蛙的能力也不用太强,最小跳跃能力最多是L,因此以[MAX,L+1)(也可以是[MAX,L],个人习惯使用左闭右开区间)作为二分搜索的区间,根据具有的能力计算出所需跳跃次数TIMES和m比较,不断二分缩小搜索范围,最后找到一个最小的能力值使TIMES<=m且TIMES接近于m。
#include <stdio.h>
#include <algorithm>
#define MAX 500000
using namespace std;
int dis[MAX+5];
int L,n,m;
int Step(int ablt){
int i,cnt,now;
i=cnt=now=0;
while(now<L){
now+=ablt;
while(dis[i+1]<=now)
i++;
now=dis[i];
cnt++;
}
return cnt;
}
int BinSearch(int low,int high){
int mid;
while(low<high){
mid=low+((high-low)>>1);
if(Step(mid)<=m)
high=mid;
else
low=mid+1;
}
return low;
}
void scani(int &num){
char ch;
int flag;
while(ch=getchar(),ch!='-'&&(ch<'0'||ch>'9'));
if(ch=='-')
flag=-1,num=0;
else
flag=1,num=ch-48;
while(ch=getchar(),ch>='0'&&ch<='9')
num=num*10+ch-48;
num*=flag;
}
int main(){
int i,ans;
dis[0]=0;
while(~scanf("%d",&L)){
scani(n),scani(m);
for(i=1;i<=n;i++)
scani(dis[i]);
sort(dis+1,dis+1+n);
dis[n+1]=L,dis[n+2]=(L<<1)+1;
ans=dis[1];
for(i=2;i<=n+1;i++)
if(dis[i]-dis[i-1]>ans)
ans=dis[i]-dis[i-1];
ans=BinSearch(ans,L+1);
printf("%d\n",ans);
}
return 0;
}
本文介绍了一项算法挑战——铁蛙三项中的跳跃比赛。比赛中,青蛙需要跨越一定宽度的河流,借助河中的石头,通过有限次跳跃抵达对岸。文章详细解释了问题背景,并提供了解决方案,包括确定最小跳跃能力和二分搜索算法的应用。
1115

被折叠的 条评论
为什么被折叠?



