题意
传送门 NC 24724
题解
二分答案,验证函数则贪心地在 1 − D 1-D 1−D 天当前愉悦值低于二分值时,在当天顺序吃巧克力,若每一天的愉悦值都不低于二分值,则满足条件。贪心算法不一定吃完全部的巧克力,那么赋合理值即可。
#include <bits/stdc++.h>
using namespace std;
#define maxn 50005
typedef long long ll;
int N, D, H[maxn], chs[maxn];
bool judge(ll x)
{
ll m = 0;
for (int t = 1, i = 0; t <= D; t++)
{
while (i < N && m < x)
{
chs[i] = t;
m += H[i++];
}
if (m < x) return 0;
m >>= 1;
}
return 1;
}
int main()
{
scanf("%d%d", &N, &D);
for (int i = 0; i < N; i++) scanf("%d", H + i);
ll lb = 0, ub = 50000000005;
while (ub - lb > 1)
{
ll mid = (lb + ub) >> 1;
if (judge(mid)) lb = mid;
else ub = mid;
}
memset(chs, 0, sizeof(chs));
judge(lb);
printf("%lld\n", lb);
for (int i = 0; i < N; i++) printf("%d\n", chs[i] == 0 ? D : chs[i]);
return 0;
}