Ultra-quicksort
Time Limit: 7000MS |
|
Memory Limit: 65536K |
Total Submissions: 67681 |
|
Accepted: 25345 |
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
Ideas
Tree-like array of naked questions, reverse-thinking, discrete processing
Each insertion point, the query under this point before the number of points are not inserted, the number of these points is the number of reverse pairs, that is, the number of steps to move
Of course, you can also use a line tree to write, but to hit more points.
Implementation code:
#pragmaComment (linker, "/stack:1024000000,1024000000")#include<iostream>#include<cstring>#include<algorithm>#include<cstdio>using namespacestd;#definell Long Long#defineLson l,m,rt<<1#defineRson m+1,r,rt<<1|1#defineMid int m = (L + r) >> 1Const intM = 5e5 +Ten;Const DoubleEPS = 1e-8;//Inline int sgn (double x) return (x > EPS)-(x <-eps);//optimization of floating-point number comparison constantsintB[m],c[m],n;intLowbit (intx) { returnx& (-x);}intGetsum (intx) { intsum =0; while(x>0) {sum+=C[x]; X-=lowbit (x); } returnsum;}voidUpdateintXintvalue) { while(x<=N) {C[x]+=value; X+=lowbit (x); }}structnode{intId,val;} A[M];BOOLCMP (node X,node y) {returnX.val <Y.val;}intMain () { while(SCANF ("%d", &n) &&N) {memset (c,0,sizeof(c)); for(inti =1; I <= N;i + +) {scanf ("%d",&a[i].val); A[i].id=i; } sort (A+1, a+n+1, CMP); for(inti =1; I <= N;i + +) B[a[i].id]=i; ll ans=0; for(inti =1; I <= N;i + +) {update (b[i],1); Ans+ =-getsum (B[i]); } cout<<ans<<Endl; } return 0;}
POJ 2299 Ultra-quicksort (tree-like array)