Minimum inversion number
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
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 Input
101 3 6 9 0 8 5 7 4 2
Sample output
16
A sequence Containing N numbers is given, and each time the first number is placed at the end of the sequence to form a new sequence. What is the smallest reverse Number of the N sequences?
Analysis: because the values of each element in the sequence are different, we only need to find the reverse number of the original sequence, and then sort the reverse number of other sequences according to the reverse number. For example, if the number of reverse orders for sequence 2 0 3 1 4 5 is 3, after placing 2 to the last edge, the number of reverse orders for each number smaller than 2 (2) is reduced by 1, the number of numbers greater than 2 (n-A [I]-1) is not changed, the number of reverse Orders of 2 becomes the number greater than that of 2 (n-A [I]-1 ). Based on this conclusion, we can roll out the number of reverse orders of other sequences, and then find the minimum value. Calculate the number of the Reverse Sequence of the original sequence using the line segment tree. For the number of I, its reverse sequence is equal to the number larger than the number already inserted into the line segment tree.
# Include <cstdio> # include <cstring> # include <algorithm> using namespace STD; const int n = 5005; # define lson L, mid, root <1 # define rson Mid + 1, R, root <1 | 1int sum [n <2], a [n]; void push_up (INT root) {sum [root] = sum [root <1] + sum [root <1 | 1];} void build_tree (int l, int R, int root) {sum [root] = 0; If (L = r) return; int mid = (L + r)> 1; build_tree (lson); build_tree (rson );} void Update (int p, int l, in T r, int root) {If (L = r) {sum [root] ++; return;} int mid = (L + r)> 1; if (P <= mid) Update (p, lson); else Update (p, rson); push_up (Root);} int query (int l, int R, int l, int R, int root) {If (L <= L & R <= r) {return sum [root];} int mid = (L + r)> 1; int ans = 0; If (L <= mid) ans + = query (L, R, lson); If (r> mid) ans + = query (L, R, rson); Return ans;} int main () {int N, I; while (~ Scanf ("% d", & N) {build_tree (0, n-1, 1); int res = 0; for (I = 0; I <n; I ++) {scanf ("% d", & A [I]); Res + = query (A [I], n-1, 0, n-1, 1 ); // query the number of data inserted into a line segment tree. The number is greater than a [I] update (A [I], 0, n-1, 1 ); // insert this number into the line segment tree} int ans = res; for (I = 0; I <n; I ++) {res + = (n-A [I]-1)-A [I]; ans = min (ANS, Res);} printf ("% d \ n ", ans);} return 0 ;}