Ultra-quicksort
Time Limit: 7000MS |
|
Memory Limit: 65536K |
Total Submissions: 56054 |
|
Accepted: 20706 |
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
/*The data structure is wonderful!!! Tree array: Each subscript with a binary representation at the end of a k consecutive 0, then the value of this coordinate is the number of 2^k and, that is, from the number of forward number 2^k and. This problem first the data discretization, so-called discretization, I now understand is data processing, so that the memory footprint of a little bit, optimize the sequence. The optimization of the problem is to sort the data first, knowing that the size relationship does not need to tube the data, and then in the tree to find out how many of each number in front of his small number and then sum. */#include<iostream>#include<stdio.h>#include<string.h>#include<string>#include<algorithm>#defineN 500010using namespacestd;structNode {Long LongVal; Long LongIP;} Fr[n];BOOLComp (node A,node b) {returna.val<B.val;}Long LongN;Long LongC[n];//record each node's andLong LongIndex[n];//Record the subscript of each node's andLong LongLowbit (Long Longx) { returnx& (-x);}Long LongGetxLong Longx) { Long Longans=0; while(x>=1) {ans+=C[x]; X-=lowbit (x); } returnans;}voidUpdateLong Longx) { while(x<=N) {c[x]+=1; X+=lowbit (x); }}intMain () {//freopen ("In.txt", "R", stdin); while(SCANF ("%lld", &n)!=eof&&N) { for(intI=1; i<=n;i++) {scanf ("%lld",&fr[i].val); Fr[i].ip=i; } Sort (Fr+1, fr+n+1, comp); for(intI=1; i<=n;i++) Index[fr[i].ip]=i; for(intI=1; i<=n;i++) c[i]=0; Long LongCur=0; for(intI=1; i<=n;i++) {update (index[i]); //Cout<<getx (Index[i]) << "";cur+= (I-Getx (Index[i])); } //cout<<endl;printf"%lld\n", cur); } return 0;}/*_ooooo_ o8888888o 88 ". "88 (| -_- |) o\ =/o ____/'---' \____. ' \\| |// `. / \\||| : ||| // / _||||| -:- ||||| - | | \\\ - /// | | | \_| ' \---/' | | \ .-\__ `-` ___/-. / ___`. .‘ /--.--\ `. . __ ."" ' < '. ___\_<|>_/___. ' > ' "". | | : `- \`.; `\ _ /`;. `/ - ` : | | \ \ `-. \_ __\/__ _/.-'//====== '-.____ '-.___\_____/___.-' ____.-' ====== ' =---= ' ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ I have a dream! A AC deram!! Orz Orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz Orz Orz or Z Orz Orz*/
POJ 2299 Ultra-quicksort