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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章