Test instructions: Swap adjacent two numbers to sort at least a few times
Ideas:
Test instructions can be converted into several reverse order numbers in the sequence
Can be thought of as bubble sort, but complexity is too high to complete with merge sort
(note n<=5000000 so that ans is used for long integer type)
#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;inta[5000000+ -];intt[5000000+ -];__int64 ans;intL,r;voidMergeintXinty) { if(y-x>1) { intm=x+ (y-x)/2; intP=x,q=m,i=x; Merge (X,m); Merge (M,y); while(p<m| | q<y) {if(q>=y| | (P<m&&a[p]<=a[q])) t[i++]=a[p++]; Else { if(p<m) {//printf ("%d%d...\n", a[p],a[q]);ans+= (M-p); } t[i++]=a[q++]; } } for(i=x;i<y;i++) a[i]=T[i]; //printf ("%d%d%d\n", x,m,y); for (i=0;i<r;i++) printf ("%d", a[i]); printf ("\n\n\n"); }}intMain () {intN; inti,j,k; while(SCANF ("%d",&N) {ans=0; for(i=0; i<n;i++) {scanf ("%d",&A[i]); } //l=0;r=n;Merge0, N); printf ("%i64d\n", ans); } return 0;}
POJ 2299 ultra-quicksort (merge sort)