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

题目描述

传送门

题解

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

代码

#include
#include
#include
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 (happyif (happyreturn false;
        ++day;
    }
    for (int i=1;i<=n;++i) sol[i]=d;
    for (int i=1;ireturn 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]);
}


总结

你可能感兴趣的:(题解,二分)