UVA 11300 Spreading the Wealth(技巧:中位数 推公式)

题目链接Spreading the Wealth
A Communist regime is trying to redistribute wealth in a village. They have have decided to sit everyone
around a circular table. First, everyone has converted all of their properties to coins of equal value,
such that the total number of coins is divisible by the number of people in the village. Finally, each
person gives a number of coins to the person on his right and a number coins to the person on his left,
such that in the end, everyone has the same number of coins. Given the number of coins of each person,
compute the minimum number of coins that must be transferred using this method so that everyone
has the same number of coins.
Input
There is a number of inputs. Each input begins with n (n < 1000001), the number of people in the
village. n lines follow, giving the number of coins of each person in the village, in counterclockwise
order around the table. The total number of coins will fit inside an unsigned 64 bit integer.
Output
For each input, output the minimum number of coins that must be transferred on a single line.
Sample Input
3
100
100
100
4
1
2
5
4
Sample Output
0
4

题目大意:
一共有 n 个人进行分钱,然后每个人有一个初始的钱 a[i],然后这 n 个人想让钱都是一样的也就是每个人分到的钱是一样的,问最少的交换钱的次数:
条件:
1):总的钱数能够被n整除
2):注意是圆桌(也就是一个环)只能相邻的给,a[1]给a[2]和a[n],

解题思路:
其实拿到这个题的时候,当时是懵的,这咋做呀,情况太多了,a[1]可以给a[2],a[2]也可以给a[1],挺麻烦的,但是你仔细想想,那我们只让a[1]给a[2],不让a[2]给a[1],这是不是可以的呢,当然是可以的,因为a[1]给了a[2] 5块钱,a[2]给了a[1] 3块钱,那么就是相当于a[1]给了a[2] 2块钱,如果是负的就证明 是a[2]给了a[1]钱,就是这个道理的,这样就稍微的简化了一点,然后我们就可以设未知数了,m是平均的钱数:

x1a[1]a[2]x1mx2a[2]a[3]x2mx3a[3]a[4]x3m...xna[n]a[1]nm

那么我们可以列一个方程组

a[1]x1+xna[2]x2+x1a[3]x3+x2...a[n]xn+xn1=m=m=m=m

一看 n 个方程组 n个未知数,可以解呀,但是解完之后发现这是必然成立的(亲证),
那么咋想呢,我们先把这未知数表示出来
x1x2x3...xn=a[1]+xnm=a[2]+x1m=a[3]+x2m=a[n]+xn1m

通过上面的式子我们可以发现,每个式子都与 xn有关,设:
C[1]C[2]C[3]...C[n1]C[n]=a[1]m=a[1]+a[2]2m=a[1]+a[2]+a[3]3m=a[1]+a[2]+..+a[n1](n1)m=0;

然后就是C[1]-xn,C[2]-xn…C[n]-xn;
只要找到一个xn使得上面的式子的值最小就行了,很显然,这个xn是C[1] C[2] C[3] … C[n]的中位数(排完序之后的)
那么我们只要求出这个了,然后再进行相加(绝对值)就行了

My Code:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cmath>
using namespace std;
const int MAXN = 1000001+5;
typedef unsigned long long ULL;
double C[MAXN],arr[MAXN];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        double sum = 0;
        for(int i=1; i<=n; i++)
        {
            scanf("%lf",&arr[i]);
            sum += arr[i];
        }
        double m = sum/n;
        C[0] = 0;
        for(int i=1; i<=n; i++)///C[n] == 0!!!
            C[i] = C[i-1] + arr[i] - m;
        sort(C+1,C+n+1);
        double x = C[(n+1)/2];
        double ret = 0;
        for(int i=1; i<=n; i++)
            ret += abs(C[i] - x);
        printf("%.0lf\n",ret);
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值