二分答案+貪心
需要注意的問題是,最後上下的巧克力要在最後一天喫完!
因爲這個問題,調了2個小時!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define LL long long
using namespace std;
int n,d,a[50009],eat[50009],t[50009];
LL s[50009];
bool check(LL x)
{
// memset(eat,0,sizeof(eat));
LL sum=0,last=0;
int j=1,day=1;
while(day<=d&&(j<=n||last>=x))
{
LL need=x-last;
if(need>0)
{
while(s[j]<sum+need&&j<=n)
{
eat[j]=day;
j++;
}
if(s[j]<sum+need) return 0;
eat[j]=day;
last=(last+s[j]-sum)/2;
sum=s[j];
j++;
day++;
}
else day++,last/=2;
}
if(day<=d) return 0;
for(int i=1;i<=j-1;i++) t[i]=eat[i];
for(int i=j;i<=n;i++) t[i]=d;
return 1;
}
int main()
{
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
scanf("%d%d",&n,&d);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
s[i]=s[i-1]+1ll*a[i];
}
LL L=0,R=5e10+7,mid,ans=L;
while(L<=R)
{
mid=(L+R)>>1;
if(check(mid)) ans=mid,L=mid+1;
else R=mid-1;
}
printf("%lld\n",ans);
for(int i=1;i<=n;i++)
printf("%d\n",t[i]);
return 0;
}