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<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int a[1000010];
int s[1000010];//用于存储各个石头块之间的距离
int l,m,n;
int text(int mid)//要注意此时的mid是最大的跳跃距离
{
int k=0;//k为跳的次数
int l=0;//l为距离(注意并不是到河岸的距离)
for(int i=0;i<n+1;i++)
{
l+=s[i];
if(l>mid)//如果已经走过的距离>mid
{
l=s[i];//更新距离
k++;//跳的步数+1
}
}
return l>mid?1<<30:k+1;//如果l>mid 说明不存在,返回2^30。存在时返回跳的次数。
}
int main()
{
while(scanf("%d%d%d",&l,&n,&m)!=EOF)
{
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
sort(a,a+n);
s[0]=a[0];//s[0]即第一块石头到河岸的距离
int max=0;
for(int i=1;i<n;i++)
{
s[i]=a[i]-a[i-1];
if(s[i]>max)
{
max=s[i];
}
}
s[n]=l-a[n-1];//最后一块石头到终点的距离
int right=l;//右界是河宽
int left=max;//左界是两个石头间隔的最大值
int mid;
while(left<right)
{
mid=(left+right)/2;
if(text(mid)<=m)
{
right=mid;
}
else
{
left=mid+1;
}
}
printf("%d\n",left);
}
return 0;
}