Original title POJ 2299:http://poj.org/problem?id=2299
Test instructions, give you an array, to count their inverse number, because the title of the array can be up to 500,000, then O (n^2) sorting algorithm do not think again, the next choice is fast row, merge, see you like
Here is a list of merge solutions:
#include"Iostream"UsingNamespace Std;Constint MAXN=500000+10;int T[MAXN];int a[MAXN];LongLong sum;voidMerge_sort(Int*a,int x,int y,Int*t){If(Y-X>1){int m=x+ (Y-X)/2;int P=xQ=m, I=x;Merge_sort(AXMT);Merge_sort(AMYT);While(p<m|| Q<y){If(q>=y|| (p<m&&a[P]<a[Q])) T[I++]=a[P++];Else{Sum+=m-P; T[I++]=a[Q++];}}For(int I=x; I<y; I+ +) A[I]=t[I];}}IntMain(){int n;While(CIN>>n&&n){Sum=0;For(int I=0;i<n;i++) cin>>a[i< Span class= "Sh-symbol" >; merge_sort (A0,n,t/* for (int i=0;i<n;i++) cout<<a[i]<< '; cout<<endl;*/cout<<sum <<endl return 0;< Span class= "Sh-cbracket" >
Fourth week of training (efficient algorithm design) a topic ultra-quicksort