POJ 2299 Ultra-QuickSort (merge sort to calculate the number of reverse orders)

Source: Internet
Author: User

POJ 2299 Ultra-QuickSort (merge sort to calculate the number of reverse orders)

 

Ultra-QuickSort
Time Limit:7000 MS   Memory Limit:65536 K
Total Submissions:47235   Accepted:17258

 

Description

In this problem, you have to analyze a particle sorting algorithm. the algorithm processes a sequence of n distinct integers by swapping two 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 how swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.

Input

The input contains several test cases. every test case begins with a line that 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 not be processed.

Output

For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence.

Sample Input

59105431230

Sample Output

60

Source

Waterloo local 2005.02.05

Question link: http://poj.org/problem? Id = 2299

Calculate the number of reverse orders

Question Analysis: Previously, I used to use a tree array to calculate the number of reverse orders and add the method of merging and sorting to make it easier to implement. Merging and sorting are decomposed from top to bottom and merged from bottom to bottom, the two intervals of each merge are sorted in order, which brings us a great benefit in finding the number of reverse orders.
We divide a large interval [l, r] into [l, mid], [mid + 1, r]. Obviously, each time we only need one number in the left interval, the number of reverse orders of a number in the right interval. You do not need to consider the number of reverse orders in the left interval and the right interval, because the merge is from bottom to top, we have already obtained results in their substates for the number of backward orders in the left and right intervals. Therefore, when merging from the bottom to the top, the number of reverse orders for each layer is the number of reverse orders for the last entire interval. Very nice application, with a deeper understanding of Recursion


# Include
 
  
# Include
  
   
# Include # define ll long longusing namespace std; int const MAX = 500005; int a [MAX], n; ll ans; void Merge (int l, int mid, int r) {int I = l, j = mid + 1; while (I <= mid & j <= r) {if (a [I] <= a [j]) I ++; else {j ++; // because the left and right intervals are ordered, a [I]> a [j] indicates a [I] ~ A [mid] is greater than a [j] ans + = mid-I + 1 ;}} sort (a + l, a + r + 1); return ;} void Merge_sort (int l, int r) {if (l <r) {int mid = (l + r)/2; Merge_sort (l, mid ); merge_sort (mid + 1, r); Merge (l, mid, r);} return ;}int main () {while (scanf (% d, & n )! = EOF & n) {ans = 0; for (int I = 0; I <n; I ++) scanf (% d, & a [I]); merge_sort (0, n-1); printf (% lld, ans );}}
  
 






 

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.