(XL) Lt1-3
This problem is still a test of mathematical level, since all exchanges are limited to the exchange between two adjacent, so it is easy to n-1 a group of equations, note that this is a ring, so the nth equation can be deduced so that it cannot be counted as an effective equation group, even if all the values cannot be solved, but we note that As long as the minimum value is obtained, then the problem of finding the extremum can be expressed by x1 A variable, that is, the elimination of the element is completed. This translates to the "single-variable extremum problem".
And then there is the obvious coordinate to beg ... And then the obvious median number is the solution.
However, when programming is disgusting, I always thought my formula may be wrong, but the problem is tot did not initialize, disgusting ah disgusting ah ... So be sure to initialize must initialize!!!
Attached code:
#include <iostream>#include<cstdio>#include<algorithm>//#include <cmath>using namespacestd;Const intmaxn=1000000+ -;Long LongA[MAXN],C[MAXN];intMain () {intN; while(SCANF ("%d", &n) = =1){ Long Longtot=0;//Initialize, otherwise a fixed random value is generated ... Hey, Daddy. for(intI=1; i<=n;i++) {scanf ("%lld",&A[i]); Tot+=A[i]; } Long Longave=tot/N; c[0]=0; for(intI=1; i<n;i++) {C[i]=c[i-1]+ave-a[i+1];//cout<< "C" <<i<< ":" <<c[i]<<endl;} sort (C,c+N); Long Longmid=c[n/2]; Long Longans=0; for(intI=0; i<n;i++) Ans+=abs (mid-C[i]); printf ("%lld\n", ans); } return 0;}
I found such a problem when I was reading a book. What is the difference between fabs and abs? What library do you need? It seems that ABS does not require additional libraries.
Libraries are different, but in fact, as long as the loading of Cmath library, they can be used, they are the difference between fabs is floating point type, ABS is integral type (note, take the absolute value after rounding).
uva11300--Algorithm Competition Introductory classic Training Guide