【杭电4004】The Frog's Games Time 青蛙过河

来源:互联网 发布:Java视频 编辑:程序博客网 时间:2024/06/11 04:55
The Frog's Games
Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64u
Submit Status

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
 题意:该题题义为有一只青蛙要过河,这条河有一个宽度,河中间有若干块石头,青蛙要求在m步的限制下跳跃到河对岸去,
问在何种情况下,青蛙能够跳跃到河对岸,并且该方案中跳跃最远的一步数值最小(意思是说这种方案下对青蛙的跳跃能力的要求是最低的)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
#include<cstdio>#include<algorithm>#include<cstdlib>using namespace std;const int M=5*1e5;int L,n,m;int a[M];bool judge(int mid)//假定mid为最大步{int last=0;//记录最后一步所在位置int t=0;//最优情况下跳几次for(int i=1;i<=n+1;){if(a[i]<=last+mid)//如果小于上一次所在位置+最大步i++;//跨过一个石头再往前跳一步else//如果跨过一个石头再跳一步的距离大于上一次所在位置+最大步{if(last==a[i-1])
        //如果最后一步所在位置在上一个点处,及两个石头在同一位置,则是错误的return false;last=a[i-1];
       //如果跨过一个石头再跳一步的距离大于上一次所在位置+最大步,后退一个石头
t++;//步数++
}}
t++;
return t<=m;
}int main()
{
while(~scanf("%d%d%d",&L,&n,&m))
{
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+n+1);
a[n+1]=L;
int l=1,r=L; 
int ans;//记录最后的结果
while(l<=r)
{int mid=(l+r)/2;
if(judge(mid))
{
ans=mid; r=mid-1;
}
else
l=mid+1;
}
printf("%d\n",ans);
}
return 0;}


0 0
原创粉丝点击