您的位置:首页 > 其它

HDU:4004 The Frog's Games<二分>

2017-09-13 20:32 399 查看

The Frog's Games

[align=center]Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 7872    Accepted Submission(s): 3703

[/align]

[align=left]Problem Description[/align]
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).

 

[align=left]Input[/align]
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.

 

[align=left]Output[/align]
For each case, output a integer standing for the frog's ability at least they should have.

 

[align=left]Sample Input[/align]

6 1 2
2
25 3 3
11
2
18

 

[align=left]Sample Output[/align]

4
11

 

[align=left]Source[/align]
The 36th ACM/ICPC Asia Regional Dalian Site —— Online
Contest
 

[align=left]Recommend[/align]
lcy

题目大意:  给你多个石头距离起点的位置,给定起点到终点的距离以及你能跳的最多步数

问,能跳到对岸去的最小的最大间距

eg:

案例中,第一步跳到距离为11的,在跳到距离为18的 ,再跳到距离为25的(终点),在这种跳法,最大的间距为11,并且是所有跳法中最小的最大值

因为最大只能是起点到终点的距离,所以我们直接二分距离答案,差不多是套模板的题目,但是要多注意边界条件

#include <iostream>
#include <cstdio>
#include<set>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,l;
int a[500005];
bool check(int mid)
{
int cnt=0;
int pos=0;
for(int i=0; i<=n;)
{
if(a[i]-pos>mid)
return false;
while(a[i]-pos<=mid&&i<=n+1)
i++;//找到我可以跳的最远的点的后一个
pos=a[i-1];//跳到我可以到的最远的石子上
cnt++;
if(cnt>m)
return false;
if(i==n+1)//已经跳到了终点的石子上
return true;
}

}

int main()
{

while(~scanf("%d%d%d",&l,&n,&m))
{
memset(a,0,sizeof(a));
for(int i=0; i<n; i++)
scanf("%d",&a[i]);
sort(a,a+n);
a
=l;
a[n+1]=2*l;//给定两个边界
int ll=0,rr=l;
int ans=rr;
while(ll<=rr)
{
int mid=ll+((rr-ll)>>1);
if(check(mid))
{
rr=mid-1;
ans=min(mid,ans);
}
else
ll=mid+1;
}
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM OJ hdu