http://poj.org/problem?id=2299
Merge sort: For example now there is a sequence [l,r], we can divide this sequence into two sequences [L,mid], [mid,r], using recursion to follow the
The method gradually reduces the sequence, the order of the sub-sequence, and then the sequence of sub-sequences, and then the orderly interval of the merger, very good drop embodies the idea of division.
Number of reverse order (if there is i<j, A[i] > A[j], then a[i] and a[j] are reversed number pairs)
#include <stdio.h>#include<algorithm>#include<math.h>#include<cstring>#include<cstdlib>#include<queue>#include<iostream>using namespacestd;#defineN 500050#defineMemset (A, B) memset (A,b,sizeof (a))Long Longcont;intA[n],b[n];voidMergeintLintR) { if(l>=R)return; intMid= (L+R)/2; Merge (L,mid); Merge (Mid+1, R); intx=l; intY=mid+1, i=l; while(X<=mid | | y<=r) {if(Y>r | | (X<=mid && a[x]<A[y])) B[i++]=a[x++]; Else { if(x<=mid) Cont+=mid-x+1; B[i++]=a[y++]; } } for(i=l;i<=r;i++) A[i]=b[i];}intMain () {intN; while(SCANF ("%d",&N), N) { for(intI=0; i<n;i++) {scanf ("%d",&A[i]); } cont=0; Merge (0, N-1);///Merge Sortprintf"%lld\n", cont); } return 0;}
ultra-quicksort--poj2299 (merge sort to find inverse number pairs)