[BZOJ2016][Usaco2010]Chocolate Eating(二分)

45 篇文章 0 订阅

题目描述

传送门

题解

二分水题。字典序尽量大只是贪心地判定就行了。
但是也有一个坑点,就是最后没有吃的巧克力都要在最后一天吃完。

代码

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define LL long long
#define N 50005

const LL inf=1e18;
int n,d,b[N],sol[N];
LL ans,a[N];

bool check(LL mid)
{
    int day,cho;LL happy;
    day=1;cho=1;happy=0;
    while (day<=d)
    {
        happy>>=1;
        while (happy<mid&&cho<=n)
            happy+=a[cho],b[cho++]=day;
        if (happy<mid) return false;
        ++day;
    }
    for (int i=1;i<=n;++i) sol[i]=d;
    for (int i=1;i<cho;++i) sol[i]=b[i];
    return true;
}
LL find()
{
    LL l=1,r=inf,mid,ans=0;
    while (l<=r)
    {
        mid=(l+r)>>1;
        if (check(mid)) ans=mid,l=mid+1;
        else r=mid-1;
    }
    return ans;
}
int main()
{
    scanf("%d%d",&n,&d);
    for (int i=1;i<=n;++i) scanf("%lld",&a[i]);
    for (int i=1;i<=n;++i) sol[i]=d;
    ans=find();
    printf("%lld\n",ans);
    for (int i=1;i<=n;++i)
        printf("%d\n",sol[i]);
}


总结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值