转换等价处理

[NOIP2015 提高组] 跳石头 - 洛谷

最后一颗不能动的石头让我怀疑二分贪心策略的正确性,但仍是正确的,因为出现这种情况,去掉当下 的石头可以解决,若不能去掉,则一定完不成,因为贪心一定是最小花费的最优情况,所以对与check仍旧等价(因为只差一个,所以既然不存在最优,这个就一定能排到最优)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inmax=40000008;
char IN[inmax+5],*ipos;
#define read() (strtol(ipos,&ipos,10))
const double eps=1e-6;
ll a[50005],n,m,s;
bool check(ll x)
{
    int cnt=0;
    int now=0;
    for(int i=1;i<=n+1;++i)
    {
        if(a[i]-now<x)
        {
            cnt++;
        }
        else {
            now=a[i];
        }
    }
    return cnt<=m;
}
int main ()
{
    fread(IN,inmax,1,stdin);ipos=IN;
    s=read(),n=read(),m=read();
    ll l=1,r=s,mid;
    for(int i=1;i<=n;++i)
    {
        a[i]=read();
    }
    a[n+1]=s;
    ll ans=0;
    while(l<=r)
    {
        mid=(l+r)>>1;
        if(check(mid))
        {
            l=mid+1;
            ans=mid;
        }
        else r=mid-1;
    }
    cout<<ans;
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值