uva 11300 - Spreading the Wealth
这个题目的证明挺美的;
把分金币问题变成了一个数轴上点的距离问题;
代码:
1 #include<cstdio> 2 #include<algorithm> 3 #define ll long long 4 #define maxn 1000009 5 using namespace std; 6 7 ll a[maxn],b[maxn],sum,m; 8 9 int main() 10 { 11 int n; 12 while(scanf("%d",&n)!=EOF) 13 { 14 sum=0; 15 for(int i=0;i<n;i++){scanf("%lld",&a[i]);sum+=a[i];} 16 m=sum/n; 17 b[0]=0; 18 for(int i=1;i<n;i++)b[i]=b[i-1]+a[i]-m; 19 sort(b,b+n); 20 ll mid=b[n/2],ans=0; 21 for(int i=0;i<n;i++)ans+=abs(mid-b[i]); 22 printf("%lld\n",ans); 23 } 24 return 0; 25 }