标签:
1 /*
2 假设x1为1号给n号的金币数(逆时针),下面类似
3 a[1] - x1 + x2 = m(平均数) 得x2 = x1 + m - a[1] = x1 - c1; //规定c1 = a[1] - m,下面类似
4 a[2] - x2 + x3 = m ,x3 = m + x2 - a[2] = m + (m + x1 - a[1]) - a[2] = 2 * m + x1 - a[1] - a[2] = x1 - c2;
5 a[3] - x3 + x4 = m ,x4 = m + x3 - a[3] =............................= 3 * m + x1 - a[1] - a[2] - a[3] = x1 - c3;
6 而我们求得是|x1|+|x2|+|x3|+。。。。+|xn|
7 把上边的公式带入,每一项都会变成|x1-Ci|的形式,那就变成了:在数轴上有n个点,求到他们的距离和最小的点是谁?
8 然后结论是x1=Ci的中位数。
9 中位数证明:http://blog.csdn.net/zhengnanlee/article/details/8915098
10 */
11 #include <cstdio>
12 #include <algorithm>
13 #include <iostream>
14 #include <cstring>
15 #include <string>
16 #include <cmath>
17 using namespace std;
18
19 const int MAXN = 1e6 + 10;
20 const int INF = 0x3f3f3f3f;
21 int a[MAXN], c[MAXN];
22
23 int main(void) //UVA 11300 Spreading the Wealth
24 {
25 //freopen ("UVA_11300.in", "r", stdin);
26
27 int n;
28 while (scanf ("%d", &n) == 1)
29 {
30 long long sum = 0; int ave = 0;
31 for (int i=1; i<=n; ++i)
32 {
33 scanf ("%d", &a[i]);
34 sum += a[i];
35 }
36 ave = sum / n;
37 c[0] = 0;
38 for (int i=1; i<=n; ++i)
39 {
40 c[i] = c[i-1] + a[i] - ave;
41 }
42
43 sort (c+1, c+1+n);
44 int x = c[n/2]; long long ans = 0;
45 for (int i=1; i<=n; ++i) ans += abs (x - c[i]);
46
47 printf ("%lld\n", ans);
48 }
49
50 return 0;
51 }
数学/思维 UVA 11300 Spreading the Wealth
标签:
原文地址:http://www.cnblogs.com/Running-Time/p/4658578.html