NC24724 [USACO 2010 Feb S]Chocolate Eating
题目
题目描述
Bessie has received chocolates from the bulls, but doesn't want to eat them too quickly, so she wants to plan out her chocolate eating schedule for the next days in order to maximize her minimum happiness level over the set of those days.
Bessie's happiness level is an integer that starts at 0 and halves (rounding down if necessary) over night as she sleeps. However, when she eats chocolate i, her happiness level increases by integer (1 <= <= 1,000,000). If she eats chocolates on a day, her happiness for that day is considered the happiness level after she eats the chocolates. Bessie insists that she eat the chocolates in the order that she received them.
If more than one optimal solution exists, print any one of them.
Consider a sequence of 5 chocolates to be eaten over a period of 5 days; they respectively bring happiness (10, 40, 13, 22, 7).
If Bessie eats the first chocolate (10 happiness) on the first day and then waits to eat the others, her happiness level is 10 after the first day.
Here is the complete schedule which turns out to maximize her minimum happiness: Day Wakeup happiness Happiness from eating Bedtime happiness 1 0 10+40 50 2 25 --- 25 3 12 13 25 4 12 22 34 5 17 7 24 The minimum bedtime happiness is 24, which turns out to be the best Bessie can do.
输入描述
- Line 1: Two space separated integers: N and D
- Lines 2..N+1: Line i+1 contains a single integer:
输出描述
- Line 1: A single integer, the highest Bessie's minimum happiness can be over the next D days
- Lines 2..N+1: Line i+1 contains an integer that is the day on which Bessie eats chocolate i
示例1
输入
5 5 10 40 13 22 7
输出
24 1 1 3 4 5
题解
知识点:二分。
二分睡前快乐,起床后快乐不达标就吃巧克力,达标就不管,中间记得记录吃到第几个巧克力。
坑点:最终答案是要把巧克力在最后一刻全吃完,所以如果达标但是巧克力没吃完,记得都输出在最后一天。
时间复杂度
空间复杂度
代码
#include <bits/stdc++.h> #define ll long long using namespace std; int N, D; int H[50007]; bool flag; vector<int> ans(50007); bool check(ll mid) { ll h = 0; int cnt = 0; for (int i = 0;i < D;i++) { h >>= 1; while (h < mid && cnt < N) { h += H[cnt++]; if (flag) ans[cnt - 1] = i + 1; } if (h < mid) return false; } return true; } int main() { std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> N >> D; for (int i = 0;i < N;i++) cin >> H[i]; ll l = 0, r = 1e12; while (l <= r) { ll mid = l + r >> 1; if (check(mid)) l = mid + 1; else r = mid - 1; } flag = true; check(r); cout << r << '\n'; for (int i = 0;i < N;i++) cout << (ans[i] ? ans[i] : D) << '\n'; return 0; }
本文来自博客园,作者:空白菌,转载请注明原文链接:https://www.cnblogs.com/BlankYang/p/16420478.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧