zoukankan      html  css  js  c++  java
  • 数学/思维 UVA 11300 Spreading the Wealth

    题目传送门

     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
    ", ans);
    48     }
    49 
    50     return 0;
    51 }
    编译人生,运行世界!
  • 相关阅读:
    JAVA反射机制--静态加载与动态加载
    MyEclipse导入Hibernate出现Path must include project and resource;/project name
    服务器和java程序的桥梁--jdbc/hibernate
    AndroidStudio快捷键大全
    AndroidStudio怎么实现微信分享功能
    《一面》
    java泛型
    java设计模式-Observer(2)
    java设计模式-Observe
    HashSet的自定义实现
  • 原文地址:https://www.cnblogs.com/Running-Time/p/4658578.html
Copyright © 2011-2022 走看看