POJ 2299 ultra-quicksort (merge sort, reverse order number) __ Sort

Source: Internet
Author: User
Description

In this problem, your have to analyze a particular 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 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 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. The 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

5
9
1
0
5
4 3 1 2 3 0


Sample Output

6
0


the

Find the minimum number of adjacent exchanges required to sort a sequence.


train of Thought

The title can be converted to the sum of the inverse numbers of all the numbers in the array , because N is a larger relationship, so the algorithm of O (n2) O (n^2) is discarded instead of the merge sort.

Generally such topics can be sorted by merging, or tree-like arrays can be.


Suppose that the current merge sort of two sequences is

1 3 4 9

2 5 7 8

Take the number 3, 2,3>2, note 3 after all the number is greater than 2 (M-P), because the sequence 11 is set in the sequence 2 before, then this explanation is the reverse number slightly.

In addition, when the sequence 2 is all sorted out, the sequence 1 has 9 left, at which point 9 and all subsequent numbers are larger than the sequence 2 (y-m).


AC Code

#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <map
> #include <set> #include <algorithm> using namespace std;
__int64 Total,n;
int a[500005],t[500005];
        void Merge_sort (int x,int y) {if (y-x>1) {int m=x+ (y-x)/2;
        int p=x,q=m,i=x;
        Merge_sort (X,M);
        Merge_sort (M,y); while (p<m&&q<y) {if (A[p]>a[q])//Description A[p] The number behind is larger than A[q] {t
                Otal+=m-p;
            T[i++]=a[q++];
            else {total+=q-m//a[q] is earlier than a[p] small t[i++]=a[p++];
        } while (Q<y) {t[i++]=a[q++];     while (p<m) {total+=y-m;
        If A[P] There is still surplus, that is, the rest is greater than a[q] t[i++]=a[p++];
    for (int j=x; j<y; j + +) A[j]=t[j]; int main (int argc, char *argv[]) {while(~SCANF ("%i64d", &n) &&n)
        {memset (a,0,sizeof (A));
        memset (t,0,sizeof (T));
        total=0;
        for (int i=0; i<n; i++) scanf ("%d", &a[i]);
        Merge_sort (0,n);  printf ("%i64d\n", TOTAL/2);
Divided by 2 is computed because the front is larger than I, the back is smaller than I, that is, the reverse number of twice times} return 0; }

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.