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
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.

The Input

There is a number of inputs. Each input begins with n(n<1000001), the number of people in the village. nlines 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.

The 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

【题意】

【解析】

【代码】

/*********************************
*   日期:2013-11-22
*   作者:SJF0115
*   题号: 题目11300 - Spreading the Wealth
*   来源:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=25&page=show_problem&problem=2275
*   结果:AC
*   来源:UVA
*   总结:
**********************************/
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;

#define N 1000000 + 10

long long Money[N],C[N],total,M,FirstMoney,ans;

int main() {
    int i,n;
    while(scanf("%d",&n) != EOF){
        total = 0;
        //每人分得的金币
        for(i = 0;i < n;i++){
            scanf("%lld",&Money[i]);
            //总金额
            total+= Money[i];
        }
        //均摊后的金币
        M = total / n;
        C[0] = 0;
        for(i = 1;i < n;i++){
            C[i] = C[i-1] + Money[i-1] - M;
        }
        //排序
        sort(C,C+n);
        //取中位数
        FirstMoney = C[n/2];
        ans = 0;
        //计算转移的金币总额
        for(i = 0;i < n;i++){
            ans += abs(FirstMoney - C[i]);
        }
        printf("%lld\n",ans);
    }//while
    return 0;
}
时间: 2024-09-30 23:14:08

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

点击打开链接uva 11300 思路:数学分析+贪心 分析: 1 首先最终每个人的金币数量可以计算出来,它等于金币总数除以人数n.接下来我们用m来表示每人的最终的金币数 2 现在假设编号为i的人初始化为Ai枚金币,Xi表示第i个人给第i-1个人Xi枚金币,对于第一个人来说他是给第n个人. 3 根据第二点可以知道                                                                                              

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