Minimum Inversion number
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 16948 Accepted Submission (s): 10292
Problem DescriptionThe Inversion number of a given number sequence A1, A2, ..., the number of pairs (AI, aj) that SA Tisfy 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'll Obtain another sequence. There is 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 is 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 the 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 to a single line.
Sample Input101 3 6 9 0 8 5 7 4 2
Sample Output16
Authorchen, Gaoli
Source ZOJ Monthly, January 2003 test instructions: The number of reverse-order numbers of the smallest number of reverse-order numbers in the array of all transformations. Puzzle: The tree-like array to find the most initial reverse number, you can transform all the reverse order number, the first is a[i], then when it is put to the last go, the whole sequence in reverse order number of n-a[i], less a[i]-1.
#include <iostream>#include<stdio.h>#include<string.h>#include<stack>#include<vector>#include<algorithm>using namespacestd;Const intN =5005;intA[n],b[n],c[n],n;intLowbit (intx) { returnx& (-x);}voidUpdateintIdxintv) { for(inti=idx;i<=n;i+=lowbit (i)) {C[i]+=W; }}intGetsum (intidx) { intsum =0; for(inti=idx;i>=1; i-=lowbit (i)) {Sum+=C[i]; } returnsum;}intMain () { while(SCANF ("%d", &n)! =EOF) { for(intI=1; i<=n;i++) {scanf ("%d",&A[i]); A[i]++; } memset (c,0,sizeof(c)); /*for (int i=1;i<=n;i++) {int id = 1; for (int j=i;j<=n;j++) {b[id++]=a[j]; } for (int j=1;j<i;j++) {b[id++]=a[j]; } int cnt = 0; for (int j=1;j<=n;j++) {update (b[j],1); Cnt+=j-getsum (B[j]); } res = min (res,cnt); }*/ Long LongCNT =0; for(intI=1; i<=n;i++) {update (a[i],1); CNT+=i-getsum (A[i]); } Long Longres =CNT; for(intI=1; i<=n;i++) {CNT= cnt+ (N-a[i])-(a[i]-1); Res=min (res,cnt); } printf ("%lld\n", RES); } return 0;}
HDU 1394 (tree-like array)