The title is described in the this problem, and you has to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping, adjacent sequence elements until the sequence is Sorted in ascending order. For the input sequence
9 1 0 5 4,
Ultra-quicksort produces the output
0 1 4 5 9.
Your task is to determine what many swap operations Ultra-quicksort needs to perform in order to sort a given input sequenc E.
Enter the input contains several test cases. Every test case begins with a line this contains a single integer n < 500,000-the length of the input sequence. Each of the following n lines contains a single integer 0≤a[i]≤999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must is processed.
Output for every input sequence, your program prints a single line containing an integer number OP, the minimum number of swaps Operations necessary to sort the given input sequence.
Sample input
59105431230
Sample output
60
is to let you from small to large order, each time can only exchange adjacent elements, to find the minimum number of times
#include <bits/stdc++.h>#definell Long Long#defineINF 0x3f3f3f3fusing namespacestd;Const intm=500005;intl[m/2+2],r[m/2+2];intt[m];ll ans;intN;voidMergearr (intA[],intLeftintMidintRight ) { intI=left,j=mid+1; intk=Left ; while(i<=mid&&j<=Right ) { if(a[i]<=A[j]) t[k++]=a[i++]; Else{t[k++]=a[j++]; Ans+=mid-i+1; } } while(i<=mid) T[k++]=a[i++]; while(j<=Right ) T[k++]=a[j++]; for(inti=left;i<=right;i++) A[i]=t[i];}voidMergeSort (intA[],intLeftintRight ) { if(left<Right ) { intMid= (left+right)/2; MergeSort (A,left,mid); MergeSort (A,mid+1, right); Mergearr (A,left,mid,right); }}intMain () {intA[m]; intN,i; while(SCANF ("%d", &n) &&n!=0) {ans=0; for(intI=1; i<=n;i++) {scanf ("%d",&A[i]); } mergesort (A,1, N); printf ("%lld\n", ans); } return 0;}
"Merge sort + reverse order number" poj-2299 Ultra-quicksort