B - River Hopscotch

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, Lunits 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:   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).

题意:

这个题的意思就是有一条河,河里有N个石头,要去掉M个石头,求最小距离的最大值

分析:

我把两岸也当成了石头,所以一共N+2个是石头,去掉M个还有N+2-M,所以对可以走过的石头二分

代码:

 

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
long long int a[50005],b[50005],L,n,m,k,ans;
long long int ok(long long int x)
{
    long long int i,temp,sum;
    temp=0;
    sum=1;
    for(i=0;i<=n;i++)
    {
        if(temp+b[i]>=x){temp=0;sum++;}
        else
            temp+=b[i];
    }
    return sum;
}
int main()
{
    long long int i,j,l,r,mid,t,minn;
    scanf("%I64d%I64d%I64d",&L,&n,&m);
    k=n+2-m;
    for(i=0;i<n;i++)
        scanf("%I64d",&a[i]);
    sort(a,a+n);
    b[0]=a[0];
    minn=b[0];
    a[n]=L;
    for(i=1;i<=n;i++)
        {b[i]=a[i]-a[i-1];if(minn>b[i])minn=b[i];}
    l=0;
    r=L+1;
    while(l<=r)
    {
        mid=(l+r)/2;
        t=ok(mid);
        if(t>=k){ans=mid;l=mid+1;}
        else
            r=mid-1;
    }

    if(n==0)printf("%lld\n",L);
    else
    printf("%lld\n",ans);
}

感受:

这次我做这个题的时候没有看出来是二分,然后我就用的暴力,一个一个的删除石头,最后结果可以想象,TLE。等比完赛,我知道是二分后自己又写了一遍,发现跟题解不一样,题解上都是把删除的石头二分,我确实把能走的石头二分,然后我给wrong了。一直没有找到错,最后在n=0时找到了错,感觉太艰辛了,我的思维跟别人都不大一样,感觉自己老是走弯路,一个二分耽误了一晚上加一个美好的早晨。。。。心疼自己一秒。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值