POJ 3258 River Hopscotch (二分)

River Hopscotch
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 8423   Accepted: 3620

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 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

Line 1: Three space-separated integers:  LN, and  M 
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

Line 1: A single integer that is the maximum of the shortest distance a cow has to jump after removing  M rocks

Sample Input

25 5 2
2
14
11
21
17

Sample Output

4

题目大意:在河的两岸分别有一起点和一终点且相距L,河里有N个石头可以踩着经过,现在要移掉M个石头,使所有相邻石头之间的最小距离最大。

很简单,最大化最小值,二分答案,求上界。

要先排序。

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
typedef __int64 ll;

ll l,n,m;

const int maxn=50000+10;
ll a[maxn];

bool C(ll x){
	ll last=0,i,cnt=0;
	for(i=1;i<=n+1;i++){
		if(a[i]-a[last]<=x) cnt++;		//如果当前的石头和last石头距离不大于x,说明当前i石头可以移掉
		else last=i;				//否则更新last
	}
	return cnt<=m;
}

int main()
{
	int i;
	while(scanf("%I64d%I64d%I64d",&l,&n,&m)!=EOF){
		for(i=1;i<=n;i++)
			scanf("%I64d",&a[i]);
		a[0]=0;
		a[n+1]=l;
		sort(a,a+n+2);		//注意要先排序
		ll L=0,R=l;
		while(R-L>1){
			ll m=(L+R)/2;
			if(C(m))
				L=m;
			else
				R=m;
		}
		printf("%I64d\n",R);	//R即上界
	}
	return 0;
}


你可能感兴趣的:(poj)