POJ 2299 Ultra-quicksort (tree-like array + discretization for inverse number)

Source: Internet
Author: User

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 on order to sort a given input seq Uence.

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 SWA P operations necessary to sort the given input sequence.

Sample Input

5
9 1 0) 5 4
3
1 2 30

Sample Output

60

Give you a sort of order that lets you find out the number of reverse orders in this sort. The first question of a tree-like array of your own, which first uses the idea of discretization.
Since each number is less than 1e9, and the range of n is not more than 5e5, and we only care about the size of these numbers, we map these numbers all to the 1~n and then insert them in the orderly position.
This will not affect the last count of the reverse order. The mapped sequence is reflect
The next number of reverse order, this time to use a tree array, the function of the tree array is used to find the array prefix and, we are supposed to have an array of length n, it now each bit initialized to 0
We insert each number in the reflect into the tree array in the order of position, assuming that the number we are inserting now is number I, and its value is X
So I'm going to change the x position of the imaginary array to 1, which means that x is inserted into the sequence
So how about an ans for each time?
We can define how many of the elements in sum (x) x and less than x have been inserted
So sum (x) is the prefix of the hypothetical array we just had, and we can use a tree-like array to
For the number of inserts I, our ans+=i-sum (x), because X is inserted into the number of I, is inserted before the number of equal to X is sum (x)
The difference between the two is inserted before X, and the value is greater than the number of X
The code is as follows:
1#include <cstdio>2#include <cstring>3#include <cmath>4#include <algorithm>5 using namespacestd;6 Const intMAXN =550000;7 intREFLECT[MAXN];8 intC[MAXN];9 intN;Ten intLowbit (intx) One { A     returnx& (-x); - } - structNode the { -     intVal,pos; - }; - Node NODE[MAXN]; + BOOLCMP (Node q1,node Q2) - { +     returnq1.val<Q2.val; A } at  - voidUpdate (intx) - { -      while(x<=N) { -c[x]+=1;//Add the x position of the array we imagined to 1 and add it directly to c[x]. -X+=lowbit (x);//Find parent Node in     } - } to intGetsum (intx) + { -     intsum=0; the      while(x>0){ *sum+=C[x]; $         /*summation can be seen as the binary representation of xPanax Notoginseng temp=x; - Add c[temp] each time, and then change the value of the last 1 position of temp to 0. the it turns out to be 0 o'clock in the end. + such as: Beg 101001 A temp=101001-sum+=c[101001] the temp=101000, sum+=c[101000) + temp=100000, sum+=c[100000) - temp=000000, please . $         */ $x-=lowbit (x); -     } -     returnsum; the } - intMain ()Wuyi { the     //freopen ("De.txt", "R", stdin); -      while(SCANF ("%d", &n) &&N) { Wu          for(intI=1; i<=n;++i) { -scanf"%d",&node[i].val); Aboutnode[i].pos=i; $         } -Sort (node+1, node + n +1, CMP); -          for(intI=1; i<=n;++i) reflect[node[i].pos]=i;//discretization maps to 1~n -          for(intI=0; i<=n;++i) c[i]=0;//initializing an array of trees A         Long Longans=0; +          for(intI=1; i<=n;++i) { theUpdate (reflect[i]);//Insert a number, update it -Ans+=i-getsum (Reflect[i]);//ans, add a bit . $         } theprintf"%lld\n", ans); the     } the     return 0; the}

POJ 2299 Ultra-quicksort (tree-like array + discretization for inverse number)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.