bzoj 1045: [HAOI2008] 糖果传递【瞎搞】
感觉我的智商可能不够写题解,就直接截了hzwer的blog 地址http://hzwer.com/2656.html
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=1000005;
int n,a[N],c[N],p;
long long sum,ans;
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;
}
int main()
{
n=read();
for(int i=1;i<=n;i++)
a[i]=read(),sum+=a[i];
p=sum/n;
for(int i=2;i<=n;i++)
c[i]=c[i-1]+a[i]-p;
sort(c+1,c+n+1);
int mid=c[(n>>1)+1];
for(int i=1;i<=n;i++)
ans+=abs(c[i]-mid);
printf("%lld",ans);
return 0;
}