Ultra-quicksort Time
limit:7000MS
Memory Limit:65536KB
64bit IO Format:%i64d &%i64 U Submit Status Practice POJ 2299
Description
In this problem, 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.
Input
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 swap op Erations necessary to sort the given input sequence.
Sample Input
59105431230
Sample Output
60
Inverse number problem
Merge Sort method to use time complexity from O (N2) to O (Nlogn)
It is important to note that because the data is very large, the worst-case answer is to go beyond the int range and use a long long to save
AC Code: GITHUB
1 /*2 By:ohyee3 Github:ohyee4 Homepage:http://www.oyohyee.com5 Email:[email protected]6 Blog:http://www.cnblogs.com/ohyee/7 8 かしこいかわいい? 9 エリーチカ! Ten to write out the хорошо code OH ~ One */ A -#include <cstdio> -#include <algorithm> the#include <cstring> -#include <cmath> -#include <string> -#include <iostream> +#include <vector> -#include <list> +#include <queue> A#include <stack> at#include <map> - using namespacestd; - - //DEBUG MODE - #defineDebug 0 - in //Loops - #defineREP (n) for (int o=0;o<n;o++) to + Const intMAXN =500005; - intA[MAXN]; the * Long Longans; $ Panax Notoginseng //put the ordered A[l]~a[mid] a[mid+1]~a[r] together - voidMergeintA[],intLintMidintr) { the intPOS1 = l;//Pointer to the left + intPos2 = mid +1;//Pointer to right A int*temp =New int[R-l +1]; the intpos =0;//pointer to a temporary array + while(Pos1 <= Mid | | Pos2 <=r) { - if(Pos1 >mid) { $temp[pos++] = a[pos2++]; $ } - if(Pos2 >r) { -temp[pos++] = a[pos1++]; the } - if(Pos1 <= Mid&&pos2 <=r) {Wuyi if(A[POS1] <=A[pos2]) { thetemp[pos++] = a[pos1++]; -}Else { Wutemp[pos++] = a[pos2++]; -Ans + = mid-pos1 +1;//Exchange About } $ } - } - for(inti =0; I <= r-l;i++) -A[l + i] =Temp[i]; A } + the //merge sort to a[l]~a[r] sort - voidMergeSort (intA[],intLintr) { $ if(l<r) { the intMid = (L + r)/2; the mergesort (a,l,mid); theMergeSort (A,mid +1, R); the merge (a,l,mid,r); - } in } the the BOOLDo () { About intN; the if(SCANF ("%d", &n), n = =0) the return false; the + REP (n) -scanf"%d",&A[o]); the BayiAns =0; theMergeSort (A,0N1); theprintf"%lld\n", ans); - /*REP (n) - printf ("%d", A[o]); the printf ("\ n");*/ the the return true; the } - the intMain () { the while(Do ()); the return 0;94}
POJ 2299.ultra-quicksort