bzoj 2016: [Usaco2010]Chocolate Eating【二分+贪心】
二分答案,贪心判断,洛谷上要开long long
#include<iostream>
#include<cstdio>
using namespace std;
const int N=50005;
int n,m,a[N],b[N];
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
bool ok(long long w)
{
long long nw=0,l=1;
for(int i=1;i<=m;i++)
{
nw>>=1;
while(nw<w&&l<=n)
nw+=a[l++];
}
return nw>=w;
}
int main()
{
n=read(),m=read();
for(int i=1;i<=n;i++)
a[i]=read();
long long l=0,r=1e12,ans=0;
while(l<=r)
{
long long mid=(l+r)>>1;
if(ok(mid))
l=mid+1,ans=mid;
else
r=mid-1;
}
printf("%lld\n",ans);
long long nw=0,w=1;
for(int i=1;i<=m;i++)
{
nw>>=1;
while(nw<ans&&w<=n)
nw+=a[w++],printf("%d\n",i);
}
for(;w<=n;w++)
printf("%d\n",m);
return 0;
}