In one arrangement, if the front and back positions of a pair of numbers are opposite to the size order, that is, the previous number is greater than the subsequent number, then they are called an inverse. The total number of reverse order in a permutation is called the inverse number of the permutation. such as 2 4 3 1, 2 1,4 3,4 1,3 1 is reverse, the reverse number is 4. An integer sequence is given to find the inverse number of the sequence.
-----------------------------------------------------------------------------
The idea of binary sorting, combined with two ordered series A and a, if the sequence b first element is small then the reverse number increases the current length of sequence A.
#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;Const intN =50500;inta[2][n];intMain () {Long LongAns =0; intN;cin>>N; for(intI=0; i<n;i++) scanf ("%d", a[0]+i); BOOLFlag =false; intAid,bid,cid; for(intStride =1; stride<n;stride<<=1){ for(intI=0; i<n;i+= (stride<<1u) ) {Bid= (cid = AID = i) +Stride; intAED =std::min (bid,n); intBed = Std::min (bid+stride,n); while(aid<aed&&bid<bed) { if(a[flag][aid]<=A[flag][bid]) {a[!flag][cid++] = a[flag][aid++]; } Else{a[!flag][cid++] = a[flag][bid++]; Ans+=aed-aid; } } while(aid<aed) a[!flag][cid++] = a[flag][aid++]; while(bid<bed) a[!flag][cid++] = a[flag][bid++]; } Flag= !Flag; } printf ("%lld\n", ans); return 0;}
Algorithm Basics-Reverse number, merge sort