Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 18612 | Accepted: 7758 |
Description
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, L units 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.
Input
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.
Output
Sample Input
25 5 2 2 14 11 21 17
Sample Output
4
Hint
题意:给出l,n,m.在长l的河中有n个石头,问去掉m个石头后各点(包括起点,终点)之间的最小距离 最大可能是多少。(就是去掉m个石头,前提是使得最小距离的值尽可能大)。
二分过程中,最小距离>mid时最多可以去掉cnt个石头,当cnt=k时,我们要求的是比此时的mid值大的存在的最小距离,所以cnt==k时并没有结束。通过l增加找到下一个比mid大的存在的距离。(仔细想想表示好难懂----|||)
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<map>
#include<vector>
#include<iostream>
#define ll long long
using namespace std;
const int maxn=50010;
int l,n,m;
int a[maxn];
int er_fen(int l,int r,int k)
{
///cnt:最小距离>mid时最多可以去掉多少个
while(l<=r)
{
//printf("l=%d r=%d\n",l,r);
int last=0;
int cnt=0;
int mid=(l+r)/2;
for(int i=1;i<=n+1;i++)
{
if(a[i]-a[last]<=mid) cnt++;
else last=i;
}
//printf("\n距离是 %d时最多去掉 %d个\n",mid,cnt);
if(cnt>k) r=mid-1;///去掉的偏多,当前mid值偏大
else l=mid+1;///当前mid偏小,mid需增,当mid值与m一样时,应找比此时mid大的下一个存在的距离值
}
return l;
}
int main()
{
while(~scanf("%d%d%d",&l,&n,&m))
{
a[0]=0;
memset(a,0,sizeof(a));
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+n+1);
a[n+1]=l;
printf("%d\n",er_fen(0,l,m));
}
}