UVA_11300Spreading the Wealth

来源:互联网 发布:黑龙江网络教育 编辑:程序博客网 时间:2024/06/02 03:29



 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

310010010041254

Sample Output

04




这道题目看起来很复杂,看了题目分析之后表示这种解题方法之前真心没有用过。讲讲刘汝佳大白书上的思路吧:

1.总的人数为n,首先根据最开始每个人手中的金币数得到最后每个人手中应该拥有的金币数M

2.让每个人按照逆时针顺序编号,假设x2表示2号给了1号多少金币,x3表示3号给了2号多少金币,由于是个环,所以x1指的是1号给了n号多少金币。

3.根据上面的关系我们可以列出n个方程:

A1 - x1 + x2 = M  得到  x2 = M - A1 + x1 = x1 - C1(规定C1 = A1 - M,下面类似的)

A2 - x2 + x3 = M  得到  x3 = M - A2 + x2 = 2M - A1 - A2 + x1 = x1 - C2

A3 - x3 + x4 = M  得到  x4 = M - A3 + x3 = 3M - A1 - A2 - A3 + x1 = x1 - C3

............

An - xn + x1 = M (多余)

4.希望所有的xi的绝对值之和尽量小,即|x1| + |x1 - C1| + |x1 - C2| + |x1 - C3| +...........+ |x1 - Cn-1| 尽量小,于是转换成了x1到各点的距离之后最小

5.当x1是0与 C1~Cn-1这n个数的中位数时距离之和最小


#include<cstdio>#include<algorithm>using namespace std;long long A[1000010];long long C[1000010];int main(){    int n;    while(scanf("%d", &n) == 1){        long long sum = 0;        long long ans = 0;        for(int i = 1; i <= n; i++){            scanf("%lld", &A[i]);//首先将每个人拥有的金币数输入到数组中,并且将所有的金币值加起来            sum += A[i];        }        long long M = sum / n;//M为最终每个人应该拿到的金币数        C[0] = 0;//注意只需要求解1~n-1的Ci值        for(int i = 1; i < n; i++)            C[i] = C[i-1] + A[i] - M;//计算出Ci的值        sort(C, C+n);        long  long mid = C[n/2];//取出中位数        for(int i = 0; i < n; i++)            ans += abs(mid - C[i]);        printf("%lld\n", ans);    }    return 0;}


 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

310010010041254

Sample Output

04

0 0
原创粉丝点击