【BZOJ3293】分金币(贪心)
【BZOJ3293】分金币(贪心)
题面
题解
和上一题一样啊。
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
#define ll long long
#define MAX 100010
inline int read()
{
int x=0,t=1;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
int n,a[MAX];
ll s[MAX];
int main()
{
n=read();ll sum=0,ans=0;
for(int i=1;i<=n;++i)sum+=(a[i]=read());sum/=n;
for(int i=1;i<=n;++i)s[i]=s[i-1]-a[i]+sum;
sort(&s[1],&s[n+1]);
for(int i=1;i<=n;++i)ans+=abs(s[i]-s[(n+1)/2]);
printf("%lld\n",ans);
return 0;
}