您的位置:首页 > 其它

【HDU】-4004-The Frog's Games(二分,难)

2016-07-26 15:47 399 查看


The Frog's Games

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)

Total Submission(s): 6058    Accepted Submission(s): 2932


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 2
2
25 3 3
11
2
18

 

Sample Output

4
11

 

宇神来讲的题,思路清晰极了,代码也是那么超然不凡(因为开始没理解(核心看不懂啊!!!),受到了伤害,~~~~(>_<)~~~~ ),不太好想。

对可跳的距离二分,判断是否可以跳到和次数是否满足要求

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int a[100000];
int L,n,m;
bool dis(int x)
{
int cnt=0;				//跳的次数
int last=0;				//上一次的位置
for(int i=1;i<=n+1;)
{
if(a[i]<=last+x)	 //a[i]<=last+x可跳到a[i]寻找下一个
{
i++;			//比可跳的近,继续找下一个,直到下一个大于可跳的位置
}
else
{
if(last==a[i-1])		//注意:如果last不是更新过来的而是不可以跳到下一个就还在原位,此时就不可能
return false;
last=a[i-1];			//可以跳到的最远就是a[i-1],last更新
cnt++;					//又跳一次 ,记录加1
}
}
cnt++;							//最后总是停在a
上,到a
还需要一次
return	cnt<=m;					//跳的次数要小于等于给定的 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+1+n);
a[n+1]=L;			//最远就是L的长度,定义为a[n+1]
int l=0,r=L;
int ans;
while(r>=l)
{
int mid=(l+r)>>1;
if(dis(mid))
{
ans=mid;		//如果为真就是可满足的,记下此时满足的位置 ans
r=mid-1;		//那么每次如果跳的更远一定成立,就向前找,找小的
}
else
l=mid+1;
}
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: