Spreading the Wealth UVA - 11300(用代数法 进行数学推导:中位数)

Spreading the Wealth UVA - 11300

题意:

有n个人坐成一圈,每个人有A【i】,问怎样传递可以使每个人的金币一样,求最小传递次数。

思路:

  1. 先设置每个人最后的金币值为M。
  2. 设第 i i i个人给 i − 1 i-1 i1 x i x_i xi个金币; i + 1 i+1 i+1 i i i x i + 1 x_{i+1} xi+1个金币。
    特别地 i i i i − 1 i-1 i1,是 x i x_i xi个金币。那么反过来 i − 1 i-1 i1 i i i就是 − x i -x_i xi个金币)
    A 1 − x 1 + x 2 = M A_1-x_1+x_2=M A1x1+x2=M
    A 2 − x 2 + x 3 = M A_2-x_2+x_3=M A2x2+x3=M
    A 3 − x 3 + x 4 = M A_3-x_3+x_4=M A3x3+x4=M

    A n − x n + x 1 = M A_n-x_n+x_1=M Anxn+x1=M
  3. 求出每个 x i x_i xi
    x 2 = M − A 1 + x 1 = x 1 − c 1 x_2=M-A_1+x_1 = x_1 - c_1 x2=MA1+x1=x1c1
    x 3 = 2 M − A 1 − A 2 + x 1 = x 1 − c 2 x_3=2M-A_1-A_2+x_1 = x_1 - c_2 x3=2MA1A2+x1=x1c2
    x 4 = 3 M − A 1 − A 2 − A 3 + x 1 = x 1 − c 3 x_4=3M-A_1-A_2-A_3+x_1 = x_1 - c_3 x4=3MA1A2A3+x1=x1c3
  4. 那么ans = ∣ x 1 ∣ + ∣ x 2 ∣ + ∣ x 3 ∣ + . . . + ∣ x n ∣ |x_1|+|x_2|+|x_3|+...+|x_n| x1+x2+x3+...+xn
  5. 即 ans = ∣ x 1 ∣ + ∣ x 1 − c 1 ∣ + ∣ x 1 − c 2 ∣ + . . . + ∣ x 1 − c n − 1 ∣ |x_1|+|x_1 - c_1|+|x_1 - c_2|+...+|x_1 - c_{n-1}| x1+x1c1+x1c2+...+x1cn1
  6. x 1 x_1 x1设置为这么多数的中位数即可。

AC

#include <iostream>
#include <bits/stdc++.h>
#define For(i,x,y) for(int i=(x); i<=(y); i++)
#define fori(i,x,y) for(int i=(x); i<(y); i++)
#define rep(i,y,x) for(int i=(y); i>=(x); i--)
#define mst(x,a) memset(x,a,sizeof(x))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int,int>pa;
typedef pair<ll,ll>pai;
const int maxn = 1e6+10;
ll c[maxn];
int main()
{
    //ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n;
    while(~scanf("%d", &n)){
        ll sum = 0, x;
        For(i,1,n)scanf("%lld", &x), sum += x, c[i] = sum;
        sum /= n;
        c[0] = 0;
        fori(i,1,n)c[i] -= sum*i;
        sort(c, c+n);
        ll mid = c[n/2], ans = 0;
        fori(i,0,n)ans += abs(mid-c[i]);
        printf("%lld\n", ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值