首页 > 代码库 > UVA-11300-Spreading the Wealth

UVA-11300-Spreading the Wealth

题目:UVA-11300-Spreading the Wealth

思路:对于人i有 现有Ai-出xi+入xi+1=平均M,Ci1~i(Ai-M),得C中有x0使得0,C1~Cn-1与其差的和最小,即为answer,x0为C中位数。

wa

#include <iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<algorithm>#include<math.h>using namespace std;long long a[1000010],c[1000000];bool cmp(int x,int y){    return x>y;}int main(){    int n,i;    long long sum,m,ans;    while(scanf("%d",&n)!=EOF)    {            sum=0;            for(i=1;i<=n;i++)            {                scanf("%d",&a[i]);                sum+=a[i];            }            m=sum/n;            c[0]=0;            for(i=1;i<=n-1;i++)                c[i]=a[i]-m+c[i-1];            sort(c,c+n,cmp);            ans=0;            for(i=0;i<n;i++)                ans+=abs(c[i]-c[n/2+1]);            printf("%lld\n",ans);    }    return 0;}

 

UVA-11300-Spreading the Wealth