Minimum Inversion number
Time limit:1 Sec Memory limit:256 MB
Topic Connection
http://acm.hdu.edu.cn/showproblem.php?pid=1394
DescriptionThe inversion number of a given number sequence A1, A2, ..., 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'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 in a single line.Sample Input
10
1 3 6 9 0 8 5 7 4 2
Sample Output16
HINT
Test instructions
This interval can be changed, that is, you can throw the first number to the last, and then change it, ask this change, the smallest number of reverse order is how many
Exercises
Ah, the largest number is N, the first number is thrown to the end, then the number of reverse order reduced num[i]-1, but increased the n-num[i], then just do it!
Code:
//Qscqesze#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>#include<map>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineMAXN 100001#defineMoD 10007#defineEPS 1e-9Const intinf=0x7fffffff;//infinitely Large/*inline ll read () {int X=0,f=1;char ch=getchar (); while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;}*///**************************************************************************************intD[MAXN];intC[MAXN];intN;intT;inlineintRead () {intx=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}intLowbit (intx) { returnx&-x;}voidUpdateintXinty) { while(x<=t) {D[x]+=y; X+=lowbit (x); }}intSumintx) { ints=0; while(x>0) {s+=D[x]; X-=lowbit (x); } returns;}intNUM[MAXN];intMain () { while(SCANF ("%d", &n)! =EOF) {memset (d,0,sizeof(d)); memset (num,0,sizeof(num)); Memset (c,0,sizeof(c)); //N=read (); intans=0; T=N; for(intI=0; i<n;i++) {Num[i]=read (); Num[i]++; Ans+=num[i]-sum (num[i]-1)-1; Update (Num[i],1); } inttmp=ans; for(intI=0; i<n;i++) {tmp+=n-1-2*num[i]+2; Ans=min (Tmp,ans); } cout<<ans<<Endl; }}
HDU 1394 Minimum Inversion number in reverse order/tree Array