题目:
题解:
这个题luogu上有一句话没有:如果有多种吃法,则输出按照词典序排序后最靠后的方案
这句话提醒我们要贪心,让每一天的快乐值“刚刚”大于你二分的mid值就OK,然后剩下的巧克力都推到最后吃
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#define LL long long
using namespace std;
int n,d,a[50005],bel[50005];
bool check(LL mid)
{
int now=1,i;LL rest=0;
for (i=1;i<=d;i++)
{
rest/=2;
while (rest<mid && now<=n) rest+=a[now],now++;
if (now>n && rest<mid) return 0;
}
return 1;
}
void dfs(LL ans)
{
int now=1,i;LL rest=0;
for (i=1;i<=d;i++)
{
rest/=2;
while (rest<ans && now<=n) rest+=a[now],bel[now]=i,now++;
}
for (i=now;i<=n;i++) bel[i]=d;
}
int main()
{
int i;LL l=0,r=0,ans;
scanf("%d%d",&n,&d);
for (i=1;i<=n;i++) scanf("%d",&a[i]),r+=a[i];
while (l<=r)
{
LL mid=(l+r)>>1;
if (check(mid)) ans=mid,l=mid+1;
else r=mid-1;
}
printf("%lld\n",ans);
dfs(ans);
for (i=1;i<=n;i++) printf("%d\n",bel[i]);
}