uva 11300 Spreading the Wealth 中位数应用

#include<bits/stdc++.h>
using namespace std;

long long  x1,t,ans,m,c[1000000+5];
int main()
{
    int i,n;
    while(~scanf("%d",&n))
    {
        m=0;
        c[0]=0;
        for(i=1;i<=n;i++)
        {
            scanf("%lld",&t);
            m+=t;
            c[i]=t+c[i-1];
        }
        m/=n;
        for(i=1;i<n;i++) c[i]-=i*m;
        sort(c,c+n);
        x1=c[n/2];
        ans=0;
        for(i=0;i<n;i++)
            ans+=abs(c[i]-x1);
        printf("%lld\n",ans);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。http://xiang578.top/

posted @ 2015-09-14 18:48  xryz  阅读(98)  评论(0编辑  收藏  举报