吃巧克力

最坑的一道二分题

巧克力要全部吃完,余下的巧克力默认放在最后一天吃。

\(check\) 函数写得不够熟练啊!!!
继续努力。

#include<iostream>
#include<cstring>
#include<cstdio>
#define LL long long
#define mid ((l+r)>>1)
using namespace std;
const LL N = 50005;
LL n,m,a[N],ans[N],l,r,anst;
inline LL read()
{
    char c=getchar();
    LL ans=0,w=1;
    while((c<'0'||c>'9')&&c!='-') c=getchar();
    if(c=='-') { w=-1; c=getchar(); }
    while(c>='0'&&c<='9')
    { ans=ans*10+c-'0'; c=getchar(); }
    return ans*w;
}
bool check(LL u)
{
    LL tot=0,last=0,cnt=0,ansp=0;
    while(tot<m)
    {
        last=last/2; ansp=0;
        while(ansp+last<u)
        {
            ++cnt; ansp+=a[cnt];
            if(cnt>=n) break;
        }
        last=ansp+last;
        if(last>=u) ++tot;
        else break;
    //  if(cnt>=n) break; 直接break 会导致接下来一直减半,但还是满足条件的情况漏掉 
    }// 还好这样的点只有一个  
    if(tot>=m) return true;
    return false;
}
void print(LL u)
{
    LL tot=0,last=0,cnt=0,ansp=0;
    while(tot<m)
    {
        last=last>>1; ansp=0;
        while(ansp+last<u) ansp+=a[++cnt],printf("%lld\n",tot+1);
        last=ansp+last;
        ++tot;
    }
    if(cnt<n)// 大坑点 有意思么 
     for(LL i=cnt+1;i<=n;i++)//
      printf("%lld\n",m);// 
    return ;
}
int main()
{
    n=read(); m=read();
    for(LL i=1;i<=n;i++)
     a[i]=read(),r+=a[i];
    while(l<=r)
    {
        if(check(mid)) { anst=mid, l=mid+1; }
        else r=mid-1;
    }
    printf("%lld\n",anst);
    print(anst);
    return 0;
}

转载于:https://www.cnblogs.com/karryW/p/10877718.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值