POJ3258 River Hopscotch(最大化最小值/二分法)

10 篇文章 0 订阅

问题描述:
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 M 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: L, N, 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

ps:这题调了一晚上,我是渣渣。


分析:

读了半天题,我简单说一下题目的大概意思:一群牛过河,河上有一些石头,牛需要踩着石头才能从河的一边达到河的另一边,每个石头都有一个到start的距离,然而那个放牛的突然有一天脑子抽了,想搬走几个石头,但是他不能搬最开始的石头start和最后的一个石头end,让我们求一下搬走几个石头后所有可能情况的距离最小值的最大值。

这又是最小值,又是最大值的,很容易绕晕人啊,反正我绕晕了一会儿。我举个例子来解释一下最大化最小值:

序列一: 1 3 6 10   我们发现这个序列中相邻两个数的差最小值为2(3-1)
序列二: 2 5 9 14   我们发现这个序列中相邻两个数的差最小值为3(5-2)

那么这两个序列的最大化最小值就是2。它是二维的概念,先是内部取最小,然后外部取最大。

再回到题目分析上,因为它要求是取走石头,我当时以为情况挺复杂的,后来发现这题就是傻13啊,和牛放牛舍一模一样啊,换个马甲就以为我不认识你了吗?题目说是搬走N个石头,那么我们转换一下思路,就在N个位置上取N-M个位置放石头,相当于那M个被搬走了嘛!!
ok,水题不解释,然而我调了一晚上才过,好气哟。注意:整个数组要加上start和end石头。

代码如下:

/*最大化最小值*/ 
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 50000+10;
const int INF = 1000000000;
int D[maxn];
int L,N,M;

/*C(d)取出两个石头后其他石头相邻的位置的距离都不小于d*/
/*题目说是搬走N个石头,那么我们转换一下思路,就在N个位置上取N-M个位置放石头,相当于那M个被搬走了*/
bool C(int d)
{
    int last = 0;
    for(int i=0; i<N-M; i++)
    {
        int crt = last + 1;
        while(crt < N+1 && D[crt] - D[last] < d )
        {
            crt ++;
        }
        if(crt == N+1 ) return false;
        last = crt; 
    } 
    return true;
}

void solve()
{
    sort(D, D+N+2);
    if(N == M) printf("%d\n",L);
    else if(M == 0) printf("%d\n",D[1]);
    else{
        int lb = 0, ub = INF;
        while(ub - lb > 1)
        {
            int mid = (lb + ub) / 2;
            if(C(mid)) lb = mid;
            else ub = mid;
        }
        printf("%d\n", lb);
    }
}
int main()
{
    scanf("%d%d%d",&L,&N,&M);
    D[0] = 0;
    for(int i=1; i<N+1; i++)
    {
        scanf("%d",&D[i]);  
    }   
    D[N+1] = L;
    solve();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值