1. 程式人生 > >River Hopscotch【二分】

River Hopscotch【二分】

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, 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;
}