Minimum inversion number
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 4647 accepted submission (s): 2809
Problem descriptionthe inversion number of a given number sequence A1, A2,..., an is the number of pairs (AI, AJ) that satisfy I <j and AI> AJ.
For a given sequence of numbers A1, A2 ,..., an, if we move the first m> = 0 numbers to the end of the seqence, we will obtain another sequence. there are totally N such sequences as the following:
A1, A2,..., An-1, an (where m = 0-the initial seqence)
A2, A3,..., An, A1 (where M = 1)
A3, A4,..., An, A1, A2 (where m = 2)
...
An, A1, A2,..., An-1 (where M = N-1)
You are asked to write a program to find the minimum inversion number out of the above sequences.
Inputthe input consists of a number of test cases. each case consists of two lines: the first line contains a positive integer n (n <= 5000); the next line contains a permutation of the N integers from 0 to n-1.
Outputfor each case, output the minimum inversion number on a single line.
Sample input10 1 3 6 9 0 8 5 7 4 2
Sample output16
Authorchen, Gaoli
Sourcezoj monthly, January 2003
Recommendignatius. l this question is to find the minimum value of the number of reverse orders after the cyclic shift. In fact, it is mainly to find the number of reverse sequences. There are many methods to calculate the number of reverse orders, which can be obtained by merging and sorting. You can also use a tree array and line segment tree to calculate the number of reverse orders. After the number of reverse orders is obtained, you can directly move the first number to the last number of reverse orders. For example, if the original number of reverse orders is ans, move a [0] to the end to reduce the number of reverse orders. A [0], at the same time, increase the number of reverse orders N-A [0]-1 is ANS-A [0] + N-A [0]-1; as long as I gets the minimum value from a 0-n-1 loop. Line Segment tree practices:
/* HDU 1394g ++ 78 Ms 280 K */ # Include <Stdio. h> # Include < String . H> # Include <Algorithm> Using Namespace STD; Const Int Maxn = 5050 ; Struct Node { Int L, R; Int SUM;} segtree [maxn * 3 ]; Void Build ( Int I, Int L, Int R) {segtree [I]. L = L; segtree [I]. r = R; If (L = R) {segtree [I]. Sum = 0 ; Return ;} Int Mid = (L + r)> 1 ; Build (I < 1 , L, mid); Build (I <1 ) | 1 , Mid + 1 , R); segtree [I]. Sum = 0 ;} Void Add ( Int I, Int T, Int Val) {segtree [I]. Sum + = Val; If (Segtree [I]. L = Segtree [I]. R ){ Return ;} Int Mid = (segtree [I]. L + segtree [I]. R)> 1 ; If (T <= mid) add (I < 1 , T, Val ); Else Add (I < 1 ) | 1 , T, Val );} Int Sum ( Int I,Int L, Int R ){ If (Segtree [I]. L = L & segtree [I]. r = R) Return Segtree [I]. sum; Int Mid = (segtree [I]. L + segtree [I]. R)> 1 ; If (R <= mid) Return Sum (I < 1 , L, R ); Else If (L> mid) Return Sum (I < 1 ) | 1 , L, R ); Else Return Sum (I < 1 , L, mid) + sum (I < 1 ) | 1 , Mid + 1 , R );} Int A [maxn]; Int Main (){ // Freopen ("in.txt", "r", stdin ); // Freopen ("out.txt", "W", stdout ); Int N; While (Scanf ( " % D " , & N )! = EOF) {build ( 1 , 0 , N-1 ); For ( Int I = 0 ; I <n; I ++ ) Scanf ( " % D " ,& A [I]); Int Ans = 0 ; For ( Int I =0 ; I <n; I ++ ) {Ans + = Sum ( 1 , A [I], n- 1 ); Add ( 1 , A [I], 1 );} Int Min = Ans; For ( Int I = 0 ; I <n; I ++ ) {Ans -= A [I]; // Descending Order Ans + = n-A [I]- 1 ; If (ANS <min) min = Ans;} printf ( " % D \ n " , Min );} Return 0 ;}
Tree array:
/* HDU 1394ac g ++ 46 Ms 252 K */ # Include <Stdio. h> # Include < String . H> # Include <Algorithm> Using Namespace STD; Const Int Maxn = 5050 ; Int C [maxn]; Int N; Int Lowbit ( Int X ){ Return X &(- X );} Void Add ( Int I, Int Val ){ While (I <= N) {C [I] + = Val; I + =Lowbit (I );}} Int Sum ( Int I ){ Int S = 0 ; While (I> 0 ) {S + = C [I]; I -= Lowbit (I );} Return S ;} Int A [maxn]; Int Main (){ While (Scanf ( " % D " , & N )! = EOF ){ Int Ans = 0 ; Memset (C, 0 , Sizeof (C )); For (Int I = 1 ; I <= N; I ++ ) {Scanf ( " % D " ,& A [I]); A [I] ++ ; Ans + = Sum (N )- Sum (A [I]); add (A [I], 1 );} Int Min = Ans; For (Int I = 1 ; I <= N; I ++ ) {Ans + = N-A [I]-(A [I]- 1 ); If (ANS <min) min = Ans;} printf ( " % D \ n " , Min );} Return 0 ;}