POJ 2299 ultra-quicksort (merge sort/reverse order)

Source: Internet
Author: User

Test instructions gives you an array to find the inverse of the (I<j&&a[i]>a[j]) the number

Let's take a look at a merge sort process:
The given array is [2, 4, 5, 3, 1], the binary array is [2, 4, 5], [1, 3], assuming we have completed the sub-procedure, and now proceed to the "and" Operation of the array:


of the A array TD style= "" > of the A array of the A array
a: [2, 4, 5]
b: [1, 3]
result:[1]
Select 1 of the B array
a: [2, 4, 5]
B: [3]
result:[1, 2]
Select the 2
a: [4, 5]
B: [3]
result:[1, 2, 3] Select 3 of the B array
a: [4, 5]
b: []
result:[1, 2, 3, 4]
Select the 4
a: [5]
b: []
result:[1, 2, 3, 4, 5]
Select the 5

When we merge [2, 4, 5] and [1, 3] we can find that when we put the element k of a array into the result array, the elements of the B array that exist in result must be smaller than K.
In the original array, the position of the elements in the B array must be after K, that is, k and these elements constitute the inverse pair.
So when we put the elements in the a array, we can calculate the number of pairs in the B array for k, by calculating the number of elements in the B array in result.
And because of the recursive process, the a array and K satisfy the number of reverse pairs is also calculated. At the end of the recursion, the number of all k in the reverse order of [2, 4, 5, 3, 1] is counted.
The same is true for the other elements in a.

#include <cstdio> #include <cstring>using namespace std;const int N = 500005;int A[n], t[n], N;long long Cnt;vo ID merge (int l, int m, int r) {    int pl = L, PR = m + 1, p = 0;    while (PL <= m && pr <= r)    {        if (A[PL] <= A[PR]) t[p++] = a[pl++];        else        {            t[p++] = a[pr++];            CNT + = m + 1-pl;        }    }    while (pl<=m) t[p++] = a[pl++];    while (pr<=r) t[p++] = a[pr++];    memcpy (A + L, T, sizeof (int) *p);} void MergeSort (int l, int r) {    if (L >= R) return;    Intm = (L + r) >> 1;    MergeSort (L, m);    MergeSort (M + 1, R);    Merge (L, M, r);} int main () {while    (scanf ("%d", &n), N)    {        for (int i = 0; i < n; ++i)            scanf ("%d", &a[i]); 
   cnt = 0;        MergeSort (0, n-1);        printf ("%lld\n", CNT);    }    return 0;}

Ultra-quicksort

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


POJ 2299 ultra-quicksort (merge sort/reverse order)

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.