1#include <iostream>2#include <malloc.h>3 using namespacestd;4 Long Longans;5 voidMergeint*a,intLeintMidintRT) {6 int*sort_data= (int*) malloc (sizeof(int) * (rt-le+1));7 if(!sort_data)return;8 intI=le,j=mid+1, pt=0;9 while(i<=mid&&j<=RT) {Ten if(a[i]<=A[j]) { Onesort_data[pt++]=a[i++]; A } - Else{//exist swap action -sort_data[pt++]=a[j++]; the /* - once we swap the postion of both the a[i] and A[j], - We do change the positon of A[i+1......mid] - as a result, we do the postion of A[i,i+1,.... mid]. + */ -ans+=mid-i+1; + } A } at while(i<=mid) { -sort_data[pt++]=a[i++]; - } - while(j<=RT) { -sort_data[pt++]=a[j++]; - } in intp; - for(inti=le,p=0; i<=rt;i++,p++){ toa[i]=Sort_data[p]; + } - } the voidMergeSort (intA[],intStinted) { * if(st<ed) { $ intMid= (st+ed)/2;Panax Notoginseng mergesort (a,st,mid); -MergeSort (a,mid+1, ed); the merge (a,st,mid,ed); + } A } the intMain () { + intn,num[500010]; - while(SCANF ("%d",&N), N) { $ans=0; $ for(intI=0; i<n;i++){ -scanf"%d",&num[i]); - } theMergeSort (NUM,0, N-1); -printf"%i64d\n", ans);Wuyi } the return 0; -}
View Code
poj2299:ultra-quicksort-merge sort to solve the problem of reverse order number