Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. The excitement takes place on a long, straight river with a rock at the start and another rock at the end, Lunits away from the start (1 ≤ L ≤ 1,000,000,000). Along the river between the starting and ending rocks, N (0 ≤ N ≤ 50,000) more rocks appear, each at an integral distance Di from the start (0 < Di < L).
To play the game, each cow in turn starts at the starting rock and tries to reach the finish at the ending rock, jumping only from rock to rock. Of course, less agile cows never make it to the final rock, ending up instead in the river.
Farmer John is proud of his cows and watches this event each year. But as time goes by, he tires of watching the timid cows of the other farmers limp across the short distances between rocks placed too closely together. He plans to remove several rocks in order to increase the shortest distance a cow will have to jump to reach the end. He knows he cannot remove the starting and ending rocks, but he calculates that he has enough resources to remove up to M rocks (0 ≤ M ≤ N).
FJ wants to know exactly how much he can increase the shortest distance *before* he starts removing the rocks. Help Farmer John determine the greatest possible shortest distance a cow has to jump after removing the optimal set of M rocks.
Lines 2.. N+1: Each line contains a single integer indicating how far some rock is away from the starting rock. No two rocks share the same position.
25 5 2 2 14 11 21 17Sample Output
4Hint
题意:L长的河,有n块石头,现在农民想去掉m块石头,使奶牛跳跃的距离的最小值最大化
(0,和L,不包括在n块石头里,且它们不可以去掉)
思路:二分结果,假设它的奶牛跳的最小距离中最大的值为mid,然后去判断可以去掉多少块石头,记录为ans,同时我们需要用s记录保留下来的石头,以便下一次比较,如果ans>m 则结果可以更大,则l=mid+1,否则r=mid;需要注意的是最后需要考虑奶牛从保留的最后一块石头跳到L时的距离需要比传过去的mid的值大
代码:
#include<iostream>
#include<cstdio>#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
ll a[50010],ans;
ll L,n,m;
ll judge(ll b)
{
ll s=0;
ans=0;
for(int i=0;i<n;i++)
{
if(s+b>a[i])
{
ans++;
}
else
s=a[i];
}
if(ans>m||L-s<b)//如果去掉的石头大于m或者从保留的石头跳到L(即河时)
return 0;
else
return 1;
}
int main()
{
while(scanf("%lld%lld%lld",&L,&n,&m)!=EOF)
{
memset(a,0,sizeof(a));
for(int i=0;i<n;i++)
{
scanf("%lld",&a[i]);
}
sort(a,a+n);
ll l=0,r=L+1,mid;
mid=(l+r)/2;
while(l<r)
{
if(judge(mid))
l=mid+1;
else
r=mid;
mid=(l+r)/2;
}
printf("%lld\n",mid-1);
}
return 0;
}