Ultra-quicksort
Time Limit: 7000MS |
|
Memory Limit: 65536K |
Total Submissions: 48257 |
|
Accepted: 17610 |
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
Source
Waterloo Local 2005.02.05
This problem can be summed up a lot of places.
1: For a group of chaotic sequence, each can only exchange adjacent elements, to achieve an orderly exchange of the number of times is the original sequence number of your reverse. CF seems to always like this problem ... I've seen it three times in my memory ...
2: The original array a[i] and the t[i of the tree array] correspond to the problem :
This problem n is 500000, if the direct open array can be open, do not need discretization. But the subscript for the
tree array corresponds to the value of the original array! that is, T[i] The following table can be as big as 999,999,999! Obviously, it needs to be discretized.
3: Learn the discretization of another way of writing.
1 /*************************************************************************2 > File name:code/poj/2299.cpp3 > Author:111qqz4 > Email: [Email protected]5 > Created time:2015 August 04 Tuesday 12:27 32 seconds6 ************************************************************************/7 8#include <iostream>9#include <iomanip>Ten#include <cstdio> One#include <algorithm> A#include <cmath> -#include <cstring> -#include <string> the#include <map> -#include <Set> -#include <queue> -#include <vector> +#include <stack> - #defineY0 ABC111QQZ + #defineY1 HUST111QQZ A #defineYn hez111qqz at #defineJ1 CUTE111QQZ - #defineTM CRAZY111QQZ - #defineLR DYING111QQZ - using namespacestd; - #defineREP (i, n) for (int i=0;i<int (n); ++i) -typedefLong LongLL; intypedef unsignedLong LongULL; - Const intINF =0x7fffffff; to Const intn=5e5+7; + intN; - intT[n]; the int ref[N]; * $ structQPanax Notoginseng { - intVal,id; the }q[n]; + A the BOOLCMP (Q a,q B) + { - if(A.val<b.val)return true; $ return false; $ } - intLowbit (intx) - { the returnx& (-x); - }Wuyi voidUpdate (intXintc) the { - for(inti = x; i < N; i = i +lowbit (i)) Wu { -T[i] = T[i] +C; About } $ } -LL Sum (intx) - { -LL res =0; A for(inti = x; I >=1; i = i-lowbit (i)) + { theres = res +T[i]; - } $ returnRes; the } the intMain () the { the while(~SCANF ("%d", &n) &&N) - { inmemset (T,0,sizeof(t)); the for(inti =1; I <= N; i++ ) the { Aboutscanf"%d", &q[i].val);//the relative size cannot be disturbed when discretization theQ[i].id =i; the } theSort (q+1, q+n+1, CMP); + for(inti =1; I <= N; i++ ) - { the ref[Q[i].id] =i;Bayi } theLL ans =0; the for(inti =1; I <= N; i++ ) - { -Updateref[I],1); theAns = ans + i-sum (ref[i]); the //cout<< "ans:" <<ans<<endl; the the } -cout<<ans<<Endl; the } the return 0; the}
POJ 2299 Ultra-quicksort (tree-like array + discretization)