uva 11300 - Spreading the Wealth

点击打开链接uva 11300

思路:数学分析+贪心
分析:
1 首先最终每个人的金币数量可以计算出来,它等于金币总数除以人数n。接下来我们用m来表示每人的最终的金币数
2 现在假设编号为i的人初始化为Ai枚金币,Xi表示第i个人给第i-1个人Xi枚金币,对于第一个人来说他是给第n个人。
3 根据第二点可以知道                                                                                                          则C0 = 0
对于第一个人 A1-X1+X2 = m  X2 = m-A1+X1 = X1-C1(规定C1 = A1-m,一下类似) 则C1 = C0+A1-m
对于第二个人 A2-X2+X3 = m  X3 = m-A2+X2 = 2m-A1-A2+X1 = X1-C2  则C2 = -2m+A1+A2 = C1+A2-m

...
对于第n个人 An-Xn+X1 = m  Xn = m-An+Xn = X1-Cn 则Cn = Cn-1+An-m
那么根据题目ans = X1+X2+X3+...+Xn = X1+X1-C1+X1-C2+...+Xn-Cn。那么如果ans要最小,根据数轴X1-Cn表示的是点X1到点Cn的距离,那么ans要最小就是使得X1为这些点的“中位数”,我们只要找到C数组并且找到中位数即可。

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

typedef long long int64;
const int MAXN = 1000010;
int64 C[MAXN] , A[MAXN];

int main(){
    int n;
    while(scanf("%d" , &n) != EOF){
        int64 sum;
        sum = 0;
        for(int i = 0 ; i < n ; i++){
           scanf("%lld" , &A[i]);
           sum += A[i];
        }
        int64 m = sum/n;//求出每个人最后的金币数
        C[0] = 0;//C[0]为0
        for(int i = 1 ; i < n ; i++)
           C[i] = C[i-1]+A[i]-m;//求C数组
        sort(C , C+n);
        int64 mid;
        if(n%2 == 0)//偶数
          mid = (C[n/2]+C[n/2-1])>>1;
        else
          mid = C[n/2];
        int64 ans = 0;
        for(int i = 0 ; i < n ; i++)
          ans += abs(mid-C[i]);
        printf("%lld\n" , ans);
    }
    return 0;
}
时间: 2024-08-02 11:15:00

uva 11300 - Spreading the Wealth的相关文章

UVA 之11300 - Spreading the Wealth

Problem 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 coi

UVA之11300 - Spreading the Wealth

 F. Spreading the Wealth  Problem 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 th

UVa 10602

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1543 类型:贪心 原题: Company Macrohard has released it's new version of editor Nottoobad, which can understand a few voice commands.

UVa 10392 Factoring Large Numbers:素因子分解

10392 - Factoring Large Numbers Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=1333 One of the central ideas behind much cryptography is that factoring

UVa 10182 Bee Maja:规律&amp;amp;O(1)算法

10182 - Bee Maja Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1123 Maja is a bee. She lives in a bee hive with thousands of other bees. This bee hive c

算法题之UVA 763

Fibinary Numbers The standard interpretation of the binary number 1010 is 8 + 2 = 10. An alternate way to view the sequence ``1010'' is to use Fibonacci numbers as bases instead of powers of two. For this problem, the terms of the Fibonacci sequence

算法题:UVa 11461 Square Numbers (简单数学)

11461 - Square Numbers Time limit: 1.000 seconds http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=24 56 A square number is an integer number whose square root is also an integer.

UVa 10183 How Many Fibs? (统计斐波那契数个数&amp;amp;高精度)

10183 - How Many Fibs? Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1124 Recall the definition of the Fibonacci numbers:    f1 := 1    f2 := 2    fn :

UVa 701 The Archeologists&#039; Dilemma: 数学及枚举

701 - The Archeologists' Dilemma Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=642 An archeologist seeking proof of the presence of extraterrestrials i