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

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;
}
时间: 2024-10-06 02:33:44

River Hopscotch-[二分查找、贪心]的相关文章

poj 3258 River Hopscotch 【二分】

题目真是不好读,大意如下(知道题意就很好解了) 大致题意: 一条河长度为 L,河的起点(Start)和终点(End)分别有2块石头,S到E的距离就是L. 河中有n块石头,每块石头到S都有唯一的距离 问现在要移除m块石头(S和E除外),每次移除的是与当前最短距离相关联的石头,要求移除m块石头后,使得那时的最短距离尽可能大,输出那个最短距离. //Memory Time //420K 391MS #include<iostream> #include<algorithm> using

POJ 3258 River Hopscotch 经典二分

点击打开链接 River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6189   Accepted: 2683 Description Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a r

Can you find it?——[二分查找]

Description Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate if you can find the three numbers Ai, Bj, Ck, which satisfy the formula Ai+Bj+Ck = X. Input There are many cases. Every data case is descr

River Hopscotch(二分最大化最小值)

River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9923   Accepted: 4252 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. T

POJ 3258 River Hopscotch(二分 跳河)

Language: Default River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7990   Accepted: 3438 Description Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to

POJ 3258 River Hopscotch(二分&#183;最小距离最大)

题意  一条河两岸之间有n个石头  求取走m个石头后  使得两个石头间距离的最小值最大 感觉关键是理解题意  简单的二分  二分最小距离  看要取走多少个(cnt)石头  cnt<=m的话 说明最小距离还可以增大  这样就可以二分出答案了 #include <cstdio> #include <algorithm> using namespace std; const int N = 50005; int p[N], l, n, m; bool ok(int k) { int

C. Tavas and Karafs 二分查找+贪心

C. Tavas and Karafs 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <string> 7 #include <vector> 8 #include <set> 9 #include <map> 10

POJ 3258 River Hopscotch(二分)

River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8975   Accepted: 3859 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. T

POJ 3258 River Hopscotch (二分)

题目地址:POJ 3258 水题.二分距离,判断是否可行.需要注意的是最后一个,因为最后一个是没法移除的,所以还要倒着判断一下. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include &l