题解 | #[USACO 2010 Feb S]Chocolate Eating#

[USACO 2010 Feb S]Chocolate Eating

https://ac.nowcoder.com/acm/problem/24724

存在最后的任务完成后,巧克力仍未分配完的情况

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 10;
int a[N];
void solve()
{
    int n, d;
    cin >> n >> d;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    auto check = [&](int x) -> bool
    {
        int l = 0, pre = 0, day = 0;
        while (day < d)
        {
            int now = pre;
            while (l < n && now < x)
            {
                now += a[l];
                l++;
            }
            if (now < x)
            {
                return false;
            }
            pre = now / 2;
            day++;
        }
        return true;
    };
    auto check1 = [&](int x) -> void
    {
        int l = 0, pre = 0, day = 0;
        while (day < d)
        {
            int now = pre;
            while (l < n && now < x)
            {
                cout<<day+1<<'\n';
                now += a[l];
                l++;
            }
            pre = now / 2;
            day++;
        }
        while(l<n)
        {
            cout<<day-1<<'\n';
        }
    };
    int l = 0, r = 1e18, mid;
    while (l + 1 != r)
    {
        mid = l + r >> 1;
        if (check(mid))
        {
            l = mid;
        }
        else
        {
            r = mid;
        }
    }
    cout << l << '\n';
    check1(l);
}
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int T;
    T = 1;
    // cin >> T;
    while (T--)
        solve();
    return 0;
}
全部评论

相关推荐

冰激凌好吃:谁知盘中餐,谁都不一般😗
点赞 评论 收藏
分享
每天起床都有好消息:刘瑞熙,真的是你啊,工作经验0年,为什么要写?突出自己是0蛋吗,期望工资也不要写,给不起的没面试,给的起的压工资,其他地方也漏洞百出
最后再改一次简历
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务