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

来源:互联网 发布:澳洲导航软件 编辑:程序博客网 时间:2024/04/20 05:38

题目描述

传送门

题解

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

代码

#include<iostream>#include<cstring>#include<cstdio>using namespace std;#define LL long long#define N 50005const 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]);}

总结

0 0