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

Source: Internet
Author: User

Main topic:

That is, give you a sequence, and then let you find out how many reverse order of this sequence, the so-called reverse is for the element in this sequence is a[i]>a[j] and i<j exist.

In fact, the original question is said, give you a sequence, so that you use the fewest number of exchanges to make this sequence into a small-to-large sort.

Problem Solving Ideas:

At first, I thought of the idea of merging, but I didn't write the code.

First come to the range bar, the length of the sequence n<=500000+4. And each a[i]<=999 999 999, for Tree[i], we know that this array is definitely not fit, so

We are going to do discrete processing, about discretization of the processing, I just saw the courseware learned today,,,

Let's start by talking about what discretization is.

Discretization that is, I now have an array in which some of the elements in the array are large or small, but the queries I make and the values of an element in the sequence are irrelevant,

Then, we can use discretization to deal with, that is, for the previous array, in some case does not change the size of the original sequence of the relationship between the mapping.

  

• For example: – original array ax [-1, 120, 13, 45, 12, 12]– sort to get the [-1, 12, 13, 45, 120]– to get a new ax array after mapping [1,5,3,4,2,2] a relatively simple notation: – All operations to Save it with an array, then sort, go to weight, and the subscript in the array is the new number after the map.

 

1 voiddiscrete ()2 {3memset (data,0,sizeof(data));4      for(inti =0; I < n;i++ )5     {6Data[i] =A[i];7     }8Sort (data,data+n);9     intCC = Unique (data,data+n)-data;Ten      for(inti =0; I < n;i++ ) One     { AA[i] =1+lower_bound (Data,data+cc,a[i])-data; -     } -}

Now let's talk about how to use bit to solve reverse order numbers.

  

3. After discretization, how to use the discrete result array to manipulate the tree array to calculate the number of reverse order?

If the data is not very large, it can be inserted into the tree array,

Each insertion of a number, counting the number of smaller numbers than he,

The corresponding reverse order is i-read (A[i]),

Where I is the number that is currently inserted, which is the subscript of the inserted number.

Read (A[i]) is smaller than the number of a[i],

I-read (A[i]) is larger than the number of a[i, that is, the number of reverse order

However, if the data is large, the discretization method must be used

Assuming that the input array is 9 1 0 5 4, the result of the discretization is aa[] = {5,2,1,4,3};

Based on the intermediate results of discrete results, the process of calculating the inverse number is a process.

1, enter 5, call Update (5, 1), set 5th bit to 1

1 2 3) 4 5

0 0 0) 0 1

Do you calculate a number that is smaller than 5 on 1-5? Here is a tree-like array of Read (5) = 1 operations,

Now use the input subscript 1-read (5) = 0 To get the reverse number of 5 for 0.

2. Enter 2, call Update (2, 1), set 2nd bit to 1

1 2 3) 4 5

0 1 0) 0 1

Do you calculate a number that is smaller than 2 on 1-2? Here is a tree-like array of Read (2) = 1 operations,

Now use the input subscript 2-read (2) = 1 to get the reverse number of 2 for 1.

3. Enter 1, call Update (1, 1), set 1th bit to 1

1 2 3) 4 5

1 1 0) 0 1

Do you calculate a number that is smaller than 1 on 1-1? Here is a tree-like array of read (1) = 1 operations,

Now use the input subscript 3-read (1) = 2 to get the reverse number of 1 for 2.

4. Enter 4, call Update (4, 1), set 5th bit to 1

1 2 3) 4 5

1 1 0) 1 1

Do you calculate a number that is smaller than 4 on 1-4? Here is a tree-like array of Read (4) = 3 operations,

Now use the input subscript 4-read (4) = 1 to get the reverse number of 4 for 1.

5. Enter 3, call Update (3, 1), set 3rd bit to 1

1 2 3) 4 5

1 1 1) 1 1

Do you calculate a number that is smaller than 3 on 1-3? Here is a tree-like array read (3) = 3 operation,

Now use the input subscript 5-read (3) = 2 to get the reverse number of 3 for 2.

6.0+1+2+1+2 = 6 This is the final reverse number.

Analyze the time complexity, first use the fast sort, the time complexity is O (NLOGN),

This is followed by a loop to insert each number, inserting a number at a time, calling update () and read (), respectively

Outer loop N, update () and read () Time O (logn) = time complexity or O (NLOGN).

The final total is O (Nlogn).

Code:

1# include<iostream>2# include<cstdio>3# include<algorithm>4# include<cstring>5 6 using namespacestd;7 8# define MAX500000+49 TentypedefLong LongLL; One  A LL A[max]; - LL Data[max]; - intTree[max]; the intN; -  - voiddiscrete () - { +memset (data,0,sizeof(data)); -      for(inti =0; I < n;i++ ) +     { AData[i] =A[i]; at     } -Sort (data,data+n); -     intCC = Unique (data,data+n)-data; -      for(inti =0; I < n;i++ ) -     { -A[i] =1+lower_bound (Data,data+cc,a[i])-data; in     } - } to  +  - voidUpdate (intPosintval) the { *      while(Pos <=N) $     {Panax Notoginsengtree[pos]+=Val; -pos + = pos& (-POS); the     } + } A  the intRead (intPOS) + { -     intsum =0; $      while(pos>0 ) $     { -sum+=Tree[pos]; -pos-=pos& (-POS); the     } -     returnsum;Wuyi } the  -  Wu intMainvoid) - { About      while(cin>>N) $     { -         if(n==0 ) -              Break; -LL ans =0; A          for(inti =0; I < n;i++ ) +         { theCin>>A[i]; -         } $ discrete (); thememset (Tree,0,sizeof(tree)); the          for(inti =0; I < n;i++ ) the         { theUpdate (A[i],1); -ans+= (i+1)-read (a[i]); in         } thecout<<ans<<Endl; the     } About  the     return 0; the}

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

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.