https://vjudge.net/problem/POJ-2299
Test instructions
For a given unordered array, the array can be ordered from small to large after the least number of times the exchange of neighboring elements is obtained.
Analysis
It is obvious that the number of reverse pairs, it is necessary to think of merge sort. The inverse pairs are computed during the merge process.
#include <iostream>#include<cmath>#include<cstring>#include<queue>#include<vector>#include<cstdio>#include<algorithm>#include<map>#include<Set>#defineRep (i,e) for (int i=0;i< (e); i++)#defineREP1 (i,e) for (int i=1;i<= (e); i++)#defineREPX (i,x,e) for (int i= (x); i<= (e); i++)#defineX First#defineY Second#definePB push_back#defineMP Make_pair#defineMset (Var,val) memset (Var,val,sizeof (VAR))#defineSCD (a) scanf ("%d", &a)#defineSCDD (A, b) scanf ("%d%d", &a,&b)#defineSCDDD (a,b,c) scanf ("%d%d%d", &a,&b,&c)#definePD (a) printf ("%d\n", a)#defineSCL (a) scanf ("%lld", &a)#defineSCLL (A, b) scanf ("%lld%lld", &a,&b)#defineSclll (a,b,c) scanf ("%lld%lld%lld", &a,&b,&c)#defineIOS Ios::sync_with_stdio (false); Cin.tie (0)using namespaceStd;typedefLong Longll;template<classT>voidTest (T a) {cout<<a<<Endl;} Template<classTclassT2>voidTest (T a,t2 b) {cout<<a<<" "<<b<<Endl;} Template<classTclassT2,classT3>voidTest (T a,t2 b,t3 c) {cout<<a<<" "<<b<<" "<<c<<Endl;} Template<classT>inlineBOOLScan_d (T &ret) { CharCintSGN; if(C=getchar (), c==eof)return 0; while(c!='-'&& (c<'0'|| C>'9')) c=GetChar (); SGN= (c=='-')?-1:1; RET= (c=='-')?0:(C-'0'); while(C=getchar (), c>='0'&&c<='9') ret = ret*Ten+ (C-'0'); RET*=SGN; return 1;}//const int N = 1E6+10;Const intINF =0x3f3f3f3f;Constll INF =0x3f3f3f3f3f3f3f3fll;Constll mod =1000000000;intT;voidtestcase () {printf ("Case %d:",++T);}Const intMAXN = 5e5+5 ;Const intMAXM =550;Const DoubleEPS = 1e-8;Const DoublePI = ACOs (-1.0); ll a[maxn],tmp[maxn];ll ans;intN;voidMerge (intLowintMidintHigh ) { intI=low,j=mid+1, k=Low ; while(i<=mid&&j<=High ) { if(a[i]<=A[j]) {Tmp[k++]=a[i++]; }Else{ans+ = JK; Tmp[k+ +] = a[j++]; } } while(i<=mid) tmp[k++] = a[i++]; while(J<=high) tmp[k++] = a[j++]; for(i=low;i<=high;++i) {A[i]=Tmp[i]; }}voidMergeSort (intAintb) { if(a<b) { intMid = (a+b) >>1; MergeSort (A,mid); MergeSort (Mid+1, B); Merge (A,MID,B); }}intMain () {#ifdef LOCAL freopen ("In.txt","R", stdin);#endif //LOCAL while(~SCANF ("%d", &n) &&N) {ans=0; for(intI=0; i<n;i++) scanf ("%lld",&A[i]); MergeSort (0, N-1); printf ("%lld\n", ans); } return 0;}
POJ-2299 ultra-quicksort (merge sort)