本文主要是介绍Chocolate Eating,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这个题的意思是Bessie有n块巧克力,她想分d天吃,每吃一块可以获得Hi的快乐,快乐经过一夜后会减半,问每天最低快乐值最高是多少
#include<iostream>
using namespace std;
const long long INF = 0x3f3f3f3f3f3f3f3f;//一开始写成了int,WA了一晚上
const int maxn = 1e6;
typedef long long ll;ll n, d, ans;
ll h[maxn], c[maxn];bool judge(ll x) {ll sum = 0, cnt = 0;for (int i = 1;i <= d;i++) {sum >>= 1;while (sum < x) {sum += h[++cnt];if (cnt > d) return 0;if (x == ans)c[cnt] = i;}}return 1;
}int main() {cin >> n >> d;for (ll i = 1;i <= n;i++)cin >> h[i];ll l = 0, r = INF;while (l <= r) {//二分法ll mid = (l + r) >> 1;if (judge(mid)) ans = mid, l = mid + 1;else r = mid - 1;}cout << ans << endl;judge(ans);for (int i = 1;i <= n;i++) {if (c[i]) cout << c[i] << endl;else cout << d << endl;}return 0;
}
这篇关于Chocolate Eating的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!