Test instructions
Give you a sequence of n numbers each time you put the first number at the end to get a new sequence there are n sequences to find which sequence in these sequences contains the smallest total number of reverse order (the minimum total number of reverse order)
Analysis:
Using bit to find out the inverse number of the initial number, the first number is put in reverse order, and the positive sequence of the order.
#include <map>#include<Set>#include<list>#include<cmath>#include<queue>#include<stack>#include<cstdio>#include<vector>#include<string>#include<cctype>#include<complex>#include<cassert>#include<utility>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>using namespaceStd;typedef pair<int,int>Pii;typedefLong Longll;#defineLson l,m,rt<<1#definePi ACOs (-1.0)#defineRson m+1,r,rt<<11#defineAll 1,n,1#defineRead Freopen ("In.txt", "R", stdin)#defineN 5010Constll infll =0x3f3f3f3f3f3f3f3fll;Const intinf=0x7ffffff;Const intMoD =1000000007;intBit[n],a[n],n;intSumintx) { intnum=0; while(x>0) {num+=Bit[x]; X-= (x& (-x)); } returnnum;}voidAddintXintd) { while(x<=N) {Bit[x]+=D; X+ = (x& (-x)); }}intMain () { while(~SCANF ("%d",&N)) { for(intI=0; i<n;++i) {scanf ("%d",&A[i]); } memset (bit,0,sizeof(bit)); intTotal=0; for(inti=n-1; i>=0; i--) { Total+=sum (a[i]+1); Add (A[i]+1,1); } intminv=Total ; for(intI=0; i<n;++i) { Total+=n-1-2*A[i]; MINV=min (minv,total); } printf ("%d\n", MINV); }return 0;}
HDU 1394-minimum inversion Number (BIT)