POJ-3258 River Hopscotch

River Hopscotch

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 23160 Accepted: 9560

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

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

Source

USACO 2006 December Silver

/*
描述 
每年奶牛们都要举办各种特殊版本的跳房子比赛,包括在河里从一个岩石跳到另一个岩石。这项激动人心的活动在一条长长的笔直河道中进行,在起点和离起点L远 (1 ≤ L≤ 1,000,000,000) 的终点处均有一个岩石。在起点和终点之间,有N (0 ≤ N ≤ 50,000) 个岩石,每个岩石与起点的距离分别为Di (0 < Di < L)。

在比赛过程中,奶牛轮流从起点出发,尝试到达终点,每一步只能从一个岩石跳到另一个岩石。当然,实力不济的奶牛是没有办法完成目标的。

农夫约翰为他的奶牛们感到自豪并且年年都观看了这项比赛。但随着时间的推移,看着其他农夫的胆小奶牛们在相距很近的岩石之间缓慢前行,他感到非常厌烦。他计划移走一些岩石,使得从起点到终点的过程中,最短的跳跃距离最长。他可以移走除起点和终点外的至多M (0 ≤ M ≤ N) 个岩石。

请帮助约翰确定移走这些岩石后,最长可能的最短跳跃距离是多少?
 
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
const int maxn = 50005;
using namespace std;
int L,n,m;
int d[maxn];
int check(int mid)
{
    int start=1;
    int cnt=0;
    for(int i=1; i<=n; i++)
    {
        if(d[i]-start<mid)
            cnt++;
        else
            start=d[i];
    }
    if(cnt>m)
        return 0;
    else
        return 1;
}
void solve()
{
    int l=d[0],r=L;
    int ans;
    while(l<=r)
    {
        int mid=(l+r)/2;
        if(check(mid))
        {
            l=mid+1;
            ans=mid;
        }

        else
            r=mid-1;
    }
    cout<<ans<<endl;
}
int main()
{
    scanf("%d%d%d",&L,&n,&m);
    d[0]=1;
    d[n+1]=L;
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&d[i]);
    }
    sort(d+1,d+n+2);
//    for(int i=0;i<=n+1;i++){
//        printf("%d\n",d[i]);
//    }
    solve();
    return 0;
}

提示:在移除位于2和14的两个岩石之后,最短跳跃距离为4(从17到21或从21到25)。

又刷了一遍这道题目,当时理解这道题目费了很长的时间。

 现在简单概括一下,需要注意的有这样几点:

一个是二分函数的构造,我的函数左右边界应该怎么变,如果是check为真的话,说明移除的石头数量在当前满足基本要求,但是我还想再找一找有没有更好的方案,所以我选择去右区间(l=mid+1)找更好的方案,最后的退出条件是l<=r,因此是找到5 4的时候退出,就不再寻找了,这个时候记录这个mid值,输出就可以了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值