码迷,mamicode.com
首页 > 其他好文 > 详细

River Hopscotch-[二分查找、贪心]

时间:2016-03-23 22:18:35      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:

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

解题思路:
  一开始题意并不太好理解。稍微转换一下思路,给定N+2个石头,从中选出N+2-M个石头使他们之间最小的距离最大。
经过分析容易发现,无论如何增减,石头之间的距离是有限的,最小为当前石头之间最小距离min,最大为L。那么问题可以转换成这样:
  在[min,L]范围内搜索满足下列条件的距离dm:
  1)有且仅有N+2-M个石头(包括起始和终点处的两块石头),他们之间的最小距离为di。
    (即:任意增减一个石头,他们之间的最小距离就不是di)
  2)dm=max{di};
 注意:条件(1)(2)保证dm的唯一性。
那么我们可以二分查找di,以di为间隔依次挑选石头,数量记为cnt,
如果cnt>N+2-M,说明di选小了;
如果cnt<N+2-M,说明di选大了;
如果cnt=N+2-M,不一定满足条件(2),需要继续向上(增大)查找。

代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <ctime>
using namespace std;
#define print_time_ printf("%f\n",double(clock())/CLOCKS_PER_SEC);
#define maxn 50000
int s[maxn+5];
int L,N,M;

bool judge(int mid){
    int cnt=0;
    int i=0;
    
    while(1){
        i=lower_bound(s+i, s+N+1, s[i]+mid)-s;
            if(i<N+1&&s[N+1]-s[i]>=mid) cnt++;
            else break;
        
    }
    return cnt-(N-M)>=0;
}

int Bsearch(int a,int b){
    if(!N) return b;
    int mid=(a+b)/2;
    int ans=0;
    while(a<=b){
        bool flag=judge(mid);
        if(flag){
            if(mid>ans) ans=mid;
            a=mid+1;
            mid=(a+b)/2;
        }
        else {
            b=mid-1;
            mid=(a+b)/2;
        }
    }
    return ans;
}
int main() {
    scanf("%d%d%d",&L,&N,&M);
    for(int i=0;i<N;i++)
        scanf("%d",&s[i+1]);
    s[0]=0;s[N+1]=L;
    sort(s, s+N+2);
    int low_bound=(1<<31)-1;
    for(int i = 1; i <= N+1; i++)
    {
        low_bound = min(low_bound, s[i]-s[i-1]);
    }
    printf("%d\n",Bsearch(low_bound, L));
    //print_time_
    return 0;
}

River Hopscotch-[二分查找、贪心]

标签:

原文地址:http://www.cnblogs.com/Kiraa/p/5313103.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!