River Hopscotch【二分】

版权声明:版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/YT201758501112/article/details/83901708

River Hopscotch

 POJ - 3258 

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

题目大意:输入三个整数L,N,M,L带代表终点的距离,N代表在起点0与终点L之间有N块石头,下面N行输入的是N块石头的位置,问当拿走k石头后牛能跳的最短距离的最大值为多少。

解决方法:最大化最小值问题,典型的二分题,二分最大值,判断当前的mid能否满足。

AC代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
int arr[maxn];
int l,n,k;
bool judge(int x)
{
    int num=0,j=0;
    for(int i=1;i<=n+1;)
    {
    	if(arr[i]-arr[j]>=x)
    	{
    		i++;
    		j=i-1;
    	}
    	else
    	{
    		num++;
    		i++;
    	}
    }
    if(num<=k)
    	return true;
    else
    	return false;
}
int main() 
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    ios::sync_with_stdio(0),cin.tie(0);
    cin>>l>>n>>k;
     rep(i,1,n) {
     	cin>>arr[i];
     }
     sort(arr+1,arr+1+n);
     arr[0]=0;
     arr[n+1]=l;
     int left=0,right=l+1;
     while(right-left>1)
     {
     	int mid=(right+left)>>1;
     	if(judge(mid))
     		left=mid;
     	else
     		right=mid;
     }
     cout<<left<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/YT201758501112/article/details/83901708
今日推荐