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 Elemen T. 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 O F swap operations necessary to sort the given input sequence.
Sample Input
5
9
1
0
5
4
3
1
2
3
0
Sample Output
6
0
Exercises
The tree array to reverse the number, first discretization, or 1e9 open, and then each time to a number id[i], the tree array in this position +1, the location of the prefix and then I-sum (Id[i]) (the difference is actually id[i] position suffix and) is this number id[i] The number of reverse orders caused.
Code:
1#include <cstdio>2#include <iostream>3#include <cstring>4#include <algorithm>5 using namespacestd;6 #defineINF 0x3f3f3f3f7 #defineM (A, B) memset (A, B, sizeof (a))8 Const intN = 5e5 +Ten;9 intC[n], id[n], N;Ten structNode { One intVal, pos; A BOOL operator< (ConstNode &RHS)Const { - returnVal <Rhs.val; - } the }a[n]; - - intLowbit (intx) {returnX &-x;} - + voidAddintXintd) { - while(x <=N) { +C[X] + =D; AX + =lowbit (x); at } - } - - intSumintx) { - Long LongRET =0; - while(x) { inRET + =C[x]; -X-=lowbit (x); to } + returnret; - } the * intMain () { $ while(SCANF ("%d", &N), N) {Panax NotoginsengM (c,0); - for(inti =1; I <= N; ++i) thescanf"%d", &a[i].val), A[i].pos =i; +Sort (A +1, A +1+n); A for(inti =1; I <= N; ++i) Id[a[i].pos] =i; the Long LongAns =0; + for(inti =1; I <= N; ++i) { -Add (Id[i],1); $Ans + =-sum (id[i]); $ } -printf"%lld\n", ans); - } the - return 0;Wuyi}
ZOJ-2386 Ultra-quicksort "tree-like array for inverse number + discretization"