POJ river hopscotch

来源:互联网 发布:万户网络怎么样 编辑:程序博客网 时间:2024/04/28 01:35
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
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).



二分,神奇的特例。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;


long long l,n,m;
long long a[50005];
long long sum;


int main()
{
    int i,j;


    while(~scanf("%lld%lld%lld",&l,&n,&m))
    {
        memset(a,0,sizeof(a));
        a[n+1]=l;//将河岸看做两块石头
        for(i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
        }
        sort(a,a+n+1);
        long long mid;
        long long low=0,high=l;
        if(n==m) cout<<l<<endl;
        else
        {
            while(1)
            {
                if(mid==(low+high)/2) break;
                int cnt=0;
                mid=(low+high)/2;
                for(i=1,j=0;i<=n+1;)
                {
                    if(a[i]-a[j]<mid)
                    {
                        cnt++;
                        i++;
                    }
                    else
                    {
                        j=i;
                        i++;
                    }
                }
                if(cnt>m)//不符合条件,mid过大
                    high=mid;
                else
                    low=mid;//该mid值较小,非最优解,但符合条件
            }
            cout<<low<<endl;
        }
    }
    return 0;
}

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 孩子脸上有红血丝怎么办 脸上长了红血丝怎么办 指甲受创出血了怎么办 手指被挤压紫了怎么办 眼睛撞了有淤血怎么办 下眼底有小白点怎么办 狗的白眼球充血怎么办 眼球有出血点是怎么办 吃阿胶上火了该怎么办 胎儿胼胝体发育不良怎么办 鸡眼看到硬芯了怎么办 小脚趾起茧子疼怎么办 脚起老茧很痛怎么办 化疗后骨髓抑制严重怎么办 胃炎引起的胃胀怎么办 胃病胀肚子很鼓怎么办 小孩淋巴结发炎肚子疼痛怎么办 顺产后子宫脱垂怎么办 顺产完子宫脱垂怎么办 额头长了个鱼鳞怎么办 脸上长了很多痣怎么办 做过狐臭的疤痕怎么办 痤疮留下的红印怎么办 脸上疤掉了黑印怎么办 脸上有黑色的疤怎么办 一只眼睛外斜视怎么办 残币银行不给换怎么办 手上有多套房的怎么办 长了两层脚指甲怎么办 指甲长了两层怎么办 脚趾甲长了两层怎么办 产妇气血虚没奶怎么办 哺乳期气血不足奶水少怎么办 刚怀孕喝了啤酒怎么办 受风怎么办最快最有效 孕妇后背受风了怎么办 肩膀和后背受风怎么办 手指关节杵肿了怎么办 骨关节退行性变怎么办 疼风脚趾肿了怎么办 痛风脚右侧肿了怎么办