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).
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.
6 1 2 2 25 3 3 11 2 18
4
11
题目大意:有一条宽为L的河流,中间有n块石头,青蛙最多跳m次,求出青蛙的最小跳跃能力,即青蛙最少一步跳多少才能保证过河。题目给出了每块石头距离起始点的距离。青蛙的最小跳跃能力的最大值为L。
用分治的思想。
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=500000+5;
int a[N];
int L,n,m;
bool fun(int mid)
{
int d=0;
int k=m;
if(mid<a[0])
return false;
for(int i=1;i<=n;i++)
{
if(mid<a[i]-d&&mid>=a[i-1]-d)
{
k--;
d=a[i-1];
}
if(k<=0)
return false;
if(mid<a[i]-d)
return false;
}
return true;
}
int main()
{
while(~scanf("%d%d%d",&L,&n,&m))
{
int x;
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
a[n]=L;
int l=0,r=L,num=0,mid;
while(l<=r)
{
mid=(l+r)>>1;
if(fun(mid))
{
num=mid;
r=mid-1;
}
else
l=mid+1;
}
printf("%d\n",num);
}
return 0;
}