[BZOJ] 1045: [HAOI2008] 糖果传递

 

贪心,中位数

 

#include<iostream>
#include<cstdio>
using namespace std;
long long a[1000005],s[1000005];

int n;

intmain(){
    long long ave=0,ans=0;
    scanf("%d",&n);
    for(int i=1;i<=n;++i)
        scanf("%lld",a+i);
    for(inti=1;i<=n;++i)
        ave+=a[i];
    ave/=n;
    for(int i=1;i<=n;++i)
        s[i]=s[i-1]+a[i]-ave;
    sort(s+1,s+n+1);
    for(int i=1;i<n/2+1;++i)
        ans+=s[n/2+1]-s[i];
    for(int i=n/2+1;i<=n;++i)
        ans+=s[i]-s[n/2+1];
    printf("%lld\n",ans);
    return0;
}

 

posted @ 2018-07-30 21:08  GhostCai  阅读(92)  评论(0编辑  收藏  举报