Test instructions
Give you n number, n number is a whole arrangement of 0~n-1.
The minimum value required to count all of its forms in reverse order. All of its forms mean that the first number at the beginning of the array is constantly placed in the last face of the array.
Inverse pairs: i<j and Ai>aj
Ideas:
A tree array is also available,
Look at the code
Code:
Const intMAXN =50005;intsum[maxn<<2];intN;voidPushup (intRT) {Sum[rt]= sum[rt<<1] + sum[rt<<1|1];}voidBuildintLintRintRT) {Sum[rt]=0; if(L==R)return; intm = (L + r) >>1; Build (Lson); Build (Rson);}voidUpdateintPintLintRintRT) { if(l==R) { ++Sum[rt]; return; } intm = (L + r) >>1; if(P <=m) update (P,lson); ElseUpdate (P,rson); Pushup (RT);}intQueryintLintRintLintRintRT) { if(L<=l && r<=R) { returnSum[rt]; } intm = (L + r) >>1; intRET =0; if(L <= m) ret + =query (L,r,lson); if(R > m) ret + =query (L,r,rson); returnret;}intMain () {intA[MAXN]; while(SCANF ("%d", &n)! =EOF) {Build (0, N-1,1); intsum =0; for(intI=1; i<=n;++i) {scanf ("%d",&A[i]); Sum+ = Query (a[i]+1, N-1,0, N-1,1); Update (A[i],0, N-1,1); } //printf ("%d\n", sum); intAns =sum; for(intI=1; i<n;++i) {Sum+ = (N-a[i]-1-A[i]); Ans=min (ans,sum); } printf ("%d\n", ans); }}
HDU 1394 Minimum Inversion Number (segment tree or tree-like array)