Links: Https://uva.onlinejudge.org/index.php?///option=com_onlinejudge&Itemid=8&pa
ge=show_problem&problem=2275
When I first saw the subject, I had no idea, I didn't think the skill in the book seemed so simple.
Assuming that M is the number of gold coins that everyone has, the change in each person's coin is the influence of the neighboring person.
Assuming that the n person constitutes a ring, first assume that n=4, set X1 refers to 1th number 4th number of gold coins, then X2 representative 2nd to 1th number
How many coins, and so on.
Then for number 1th, he gave 4th gold coins, then the a1-x1 left.
No. 2nd gave him the gold coins, then the a1-x1+x2 left.
Note that the meaning of number 1th for numbers 4th and 4th is implied in the X1 symbol (very ingenious), and other similar
Since the last to be equal to M, then we can get the equation.
A1-x1+x2=m >> x2 = x1-(a1-m) = X1-c1
A2-x2+x3=m >> x3 = x2-(a2-m) =x1-a1-a2-2*m = x1-c2
A3-x3+x4=m >> x4 = x3-a1-a2-a3-3*m = x1-c3
An-xn+x1=m >> xn = x1-c (n-1)
See here we know the answer is that all Xi's absolute values are minimal.
It's obvious that this translates to a minimum of X-to-ci distance and a number of axes, but this point x is actually a sequence of CI (row
The median of the post-order), think about it a little bit.
The median can be used for many of the problems in this model
It seems so complicated, and finally so simple, the power of mathematics
The code is as follows:
1 #include <cstdio> 2 #include <math.h> 3 #include <vector> 4 #include <algorithm> 5 #define LL Long L Ong 6 using namespace std; 7 8 const int MAXN = 1e6+10LL c[maxn];10 int{ int LL a,sum,m;14 while (scanf ("%d", &n) ==1 {sum = 0; c[0] = 0; + for(int i = 1; i<=n; i++ {scanf ("%lld", &
a) C[i] = c[i-1] +
a;22 sum + = }24 M = sum/ n;25 for (int i = 1; i<n; i++) c[i]-= i*m;26 sort (C,c + n); x1 = C[n/2], ans = 0; (int i = 0; i<n; i++) ans + = ABS (x1-c[i]); printf ("%lld\n" }32 return 0;
UVA 11300 cent Gold coin