POJ-3258-River Hopscotch(二分)

题目链接:http://poj.org/problem?id=3258

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

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

题目大意:NOJ上有一道一模一样的中文题(我觉得)http://noi.openjudge.cn/ch0111/10/

直接二分结果,对于每个结果,判断可以消除得石头得个数,如果可消除得石头多了,减少mid;可消除得石头少了(或者正好,因为要取最大值),增加mid,最后特判一下最后的一个石子到岸边得距离是否符合要求

用一个二分的板子

ac:

//一页 27行就很舒服 
#include<stdio.h>
#include<string.h>  
#include<math.h>  

//#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<iostream>  
#include<algorithm>  
using namespace std;  

#define ll long long  
#define INF 0x3f3f3f3f  
#define mod 998244353
//#define max(a,b) (a)>(b)?(a):(b)
//#define min(a,b) (a)<(b)?(a):(b) 
#define clean(a,b) memset(a,b,sizeof(a))// 水印 
//std::ios::sync_with_stdio(false);

ll arr[50100];
ll L,n,m;

bool cmp(ll a,ll b)
{
	return a<b;
}

bool judge(ll mid)
{
	ll x=0,sum=0;
	for(int i=1;i<=n+1;++i)
	{
		if(arr[i]-x<mid)
			sum++;
		else
			x=arr[i];
	}
	if(sum>m)//可以移除的石子太多了 ,mid小点 
		return 0;
	return 1;//可移动的石子太少了||符合要求的石子 
}

int main()
{
	cin>>L>>n>>m;
	for(int i=1;i<=n;++i)
		scanf("%lld",&arr[i]);
	arr[n+1]=L;//加上最后的一块石头
	sort(arr+1,arr+n+2,cmp);
	ll r=L,l=0,mid,ans;
	while(r>=l)
	{
		mid=(l+r)>>1;
		if(judge(mid))
		{
			ans=mid;
			l=mid+1;
		}
		else
			r=mid-1;
	}
	printf("%lld\n",ans);
	
}

猜你喜欢

转载自blog.csdn.net/qq_40482358/article/details/81712748