Ultra-quicksort
Time limit:7000 Ms |
|
Memory limit:65536 K |
Total submissions:39767 |
|
Accepted:14336 |
Description
In this problem, you have to analyze a particle sorting algorithm. the algorithm processes a sequence of N distinct integers by swapping two 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 how swap operations ultra-quicksort needs to perform in order to sort a given input sequence.
Input
The input contains several test cases. every test case begins with a line that 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 not be processed.
Output
For every input sequence, your program prints a single line containing an integer number OP, the minimum number of swap operations necessary to sort the given input sequence.
Sample Input
59105431230
Sample output
60
Merge Sorting:
# Include <iostream> # include <cstring> using namespace STD; # define M 500005int A [m], B [m] ;__ int64 ans; void mergr_sort (INT X, int y) {If (Y-x> 1) {int M = x + (Y-x)/2; // divide int P = x, q = m, I = x; mergr_sort (x, m); // recursive mergr_sort (M, Y); // recursive while (P <M | q <Y) {If (q> = Y | (P <M & A [p] <= A [Q]) B [I ++] = A [p ++]; // copy the left half array to the temporary space else {B [I ++] = A [q ++]; // copy the right-hand array to the temporary space ans + = m-P ;}for (I = x; I <Y; I ++) A [I] = B [I] ;}} int main () {int N; while (CIN> N) {If (n = 0) break; memset (A, 0, sizeof (a); memset (B, 0, sizeof (B); int I; for (I = 1; I <= N; I ++) CIN> A [I]; ans = 0; mergr_sort (1, n + 1); printf ("% i64d \ n", ANS );} return 0 ;}
Poj 2299 ultra-quicksort