POJ - 3258 River Hopscotch(二分找最小值最大)

POJ - 3258 River Hopscotch(二分找最小值最大)

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
Line 1: Three space-separated integers: L, N, 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
Hint
Before removing any rocks, the shortest jump was a jump of 2 from 0 (the start) to 2. After removing the rocks at 2 and 14, the shortest required jump is a jump of 4 (from 17 to 21 or from 21 to 25).

题目大意:

有n+2个石头 ,起点的在0位置,终点的在L位置。中间有n个,分别在a[i]位置。
问:移除m个石头后,两个石头之间的最小距离最大是多少(起点终点的石头不可移动)

解题思路:

对a数组排序后,算出每两个石头之间的差值来。
然后二分答案,对每个答案检测是不是可行
怎么检测呢?们用一个dis来记录这个石头和上一个石头间的距离 对每个二分的mid 我们遍历这个差值数组,如果cha[]+dis小于mid 则需要移除一个石头 cnt++。如果大于 就让dis 归零。

bool judge(int x){
	int cnt = 0;
	int dis = 0;
	for(int i=1;i<=n+1;i++){
		if((dis+cha[i])<x){
			cnt++;
			dis += cha[i];
		}else{
			dis = 0;
		}
	}
	if(cnt<=m)	return true;
	else return false;
}

最后判断一下需要移除的石头数量是否小于m

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 5e4+10;
int l,n,m;
int a[maxn];
int cha[maxn];
bool judge(int x){
	int cnt = 0;
	int dis = 0;
	for(int i=1;i<=n+1;i++){
		if((dis+cha[i])<x){
			cnt++;
			dis += cha[i];
		}else{
			dis = 0;
		}
	}
	if(cnt<=m)	return true;
	else return false;
}
void show_cha(){
	for(int i=1;i<=n+1;i++){
		cout<<cha[i]<<" ";
	}
	cout<<endl;
}
int main(){
	scanf("%d%d%d",&l,&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
	}
	sort(a+1,a+1+n);
	a[n+1] = l;
	cha[1] = a[1];
	int min_dis = a[1];
	for(int i=2;i<=n+1;i++){
		cha[i] = a[i]-a[i-1];
		min_dis = min(min_dis,cha[i]);
	}

//	show_cha();
	int left = min_dis;
	int right = l;
	int ans = left;
	while(left<=right){
		int mid = (left+right)/2;
		if(judge(mid)){
			ans = mid;
			left= mid+1;
		}else{
			right = mid-1;
		}
	} 
	cout<<ans<<endl;
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章