描述
http://poj.org/problem?id=3258
给出起点和终点之间的距离L,中间有n个石子,给出第i个石子与起点之间的距离d[i],现在要去掉m个石子(不包括起终点),求距离最近的两个石子(包括起终点)之间距离的最大值.
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 10841 | Accepted: 4654 |
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
Source
分析
二分.
同 POJ 2456 , 但有一点不同:start与end位置是已经确定好的,只需要在中间n个石子中确定n-m个即可.第一个石子与a[0]即start位置比较,结束之后还要再比较a[n+1]-a[last]>=x是否成立.(也是注意点1)
#include<cstdio>
#include<algorithm>
using std :: sort; const int maxn=;
int n,l,m;
int a[maxn]; void init()
{
scanf("%d%d%d",&l,&n,&m);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
sort(a+,a+n+);
a[n+]=l;
} bool C(int x)
{
int last=;
for(int i=;i<=n-m;i++)
{
int now=last+;
while(now<=n&&(a[now]-a[last])<x) now++;
if(now>n) return false;
last=now;
}
if(a[n+]-a[last]<x) return false;
return true;
} int bsearch(int x,int y)
{
while(x<y)
{
int m=x+(y-x+)/;
if(C(m)) x=m;
else y=m-;
}
return x;
} void solve()
{
int INF=l/(n+-m);//共n+1-m个线段,最短的线段最多是l/(n+1-m)
int ans=bsearch(,INF);
printf("%d\n",ans);
} int main()
{
freopen("river.in","r",stdin);
freopen("river.out","w",stdout);
init();
solve();
fclose(stdin);
fclose(stdout);
return ;
}
注意:
1.如上所述.
2.输入数据是无序的,需先用快排进行排序.