题意:n个人坐成一圈,分金币(只能传递金币给相邻的人),使得最后每个人的金币数相等,问最少传送多少个金币
白书上讲得很清楚, 最开始不懂的是这个式子
|x1| + |x1-c1| + | x1 - c2|+-----
后来才发现要求的就是,x1 + x2 + x3 + x4 + ----
x1-c1对应x2,,
x1-c2对应x3------
然后c0是等于0的
我真是太捉急了-------------------5555555555
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector> 7 #include<map> 8 #include<set> 9 #include<queue> 10 #include<algorithm> 11 using namespace std; 12 13 typedef long long LL; 14 const int INF = (1<<30)-1; 15 const int mod=1000000007; 16 const int maxn=1000005; 17 18 int n; 19 LL a[maxn],c[maxn]; 20 21 int main(){ 22 while(scanf("%d",&n) != EOF && n){ 23 memset(a,0,sizeof(a)); 24 memset(c,0,sizeof(c)); 25 LL ret=0,M=0; 26 for(int i = 1;i <= n;i++) scanf("%lld",&a[i]),ret += a[i]; 27 M = ret / n; 28 c[0] = 0; 29 for(int i = 1;i < n;i++) c[i] = c[i-1] + a[i] - M; 30 sort(c,c+n); 31 LL x1 = c[n/2]; 32 LL ans = 0; 33 for(int i = 0;i < n;i++) ans += abs(x1 - c[i]); 34 printf("%lld ",ans); 35 } 36 return 0; 37 }