F. Spreading the wealth
Problem
A communist regime is trying to redistribute wealth in a village. they have decided to sit everyone around und 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 beginsN(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
Problem setter: Josh Bao
Link: http://uva.onlinejudge.org/external/113/11300.html
Analysis: Good questions. It mainly refers to the analysis process. For details, seeCodeAnnotations.
/* The original number of gold coins in the UVA 11300 is A1, A2, '''. the final gold coins are the average values of these numbers, set to Axi to indicate I to I + 1 the number of coins A1 + xn-x1 = Aa2 + x1-x2 = aa3 + x2-x3 = aa4 + x3-x4 = ....... an + x (n-1)-xn = A uses N-1 1 equations and X1 to represent X2, X3 ,... xnx2 = x1-(A-a2) X3 = x1-(2 * A-a2-a3 );.... the result is | X1 | + | x1-b1 | + | x1-b2 | +... + | minimum value of x1-b [n-1] |, median */ # Include <Stdio. h> # Include <Algorithm> # Include <Iostream> # Include < String . H> # Include <Math. h>Using Namespace STD; Const Int Maxn = 1000010 ; Long Long A [maxn], B [maxn]; Int Main (){ Int N; While (Scanf ( " % D " , & N) = 1 ){ Long Long Sum = 0 ; For ( Int I = 1 ; I <= N; I ++ ) {Scanf ( " % I64d " , & A [I]);// Use LLD Sum + = A [I];} Long Long A = sum/ N; B [ 0 ] = 0 ; For ( Int I = 1 ; I <n; I ++ ) B [I] = B [I- 1 ] + A-A [I +1 ]; Sort (B, B + N ); Long Long T = B [N/ 2 ]; Long Long Ans = 0 ; For ( Int I = 0 ; I <n; I ++) ans + = ABS (t- B [I]); printf ( " % I64d \ n " , ANS ); // Use LLD } Return 0 ;}