HDOJ 4004 The Frog's Games

来源:互联网 发布:js创建一个数组 编辑:程序博客网 时间:2024/05/17 09:08

The Frog's Games

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


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
 

Recommend
lcy
 


二分还不是很熟,一遇到整数二分就在玄学=_=|||

这个题一开始不会OTZ后来看学长的代码才写出来……


题意:青蛙王国有一个一年一度的比赛,比赛的内容是从河的一边跳到另一边并且不能掉到水里,并且规定了最多只能跳m次。现在水里有一些大石头距离河岸一定的距离,问青蛙最少跳多远才能赢比赛。


题解:分跳的距离,用跳的次数来判定。


#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int a[500000+10];int l,n,m;int OK(int x){int cnt=0,last=0;for(int i=1;i<=n+1;){if(a[i]<=last+x)i++;else{if(last==a[i-1])return 0;last=a[i-1];cnt++;}}cnt++;if(cnt<=m)return 1;return 0;}int main(){int i;while(~scanf("%d%d%d",&l,&n,&m)){memset(a,0,sizeof(a));for(i=1;i<=n;i++)scanf("%d",&a[i]);a[n+1]=l;sort(a,a+n+2);int ans;int left=0,right=l,mid;while(left<=right){mid=(left+right)/2;if(OK(mid)){ans=mid;right=mid-1;}else left=mid+1;}printf("%d\n",ans);}return 0;}


0 0
原创粉丝点击