Ultra-quicksort (merge sort, reverse order number)

Source: Internet
Author: User

The title is described in the this problem, and 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.

Enter 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 swaps Operations necessary to sort the given input sequence.

Sample input
59105431230

Sample output
60

#include <bits/stdc++.h>using namespacestd;Const intMAXN = 5e5 + +;Const intINF =0x3f3f3f3f;intS[MAXN], T[MAXN];Long LongCNT =0;intN;voidMergeintL1,intR1,intL2,intR2) {    intK =0;  while(L1 <= R1 && L2 <=R2) {        if(S[l2] < S[L1]) t[++k] = s[l2++], cnt + = R1-l1 +1; //if S[L2] small, then l1~r1 are larger than a[j], they will and a[j] form the reverse        ElseT[++k] = s[l1++]; }     while(L1 <= r1) T[++k] = s[l1++];  while(L2 <= r2) t[++k] = s[l2++];  for(inti = k, j = R2; I >=1; I--, j--) {//the final value of J is not necessarily 1~r2S[J] =T[i]; }}voidMergeSort (intLintr) {intMid = (L + r)/2; if(L <r) {mergesort (L, mid); MergeSort (Mid+1, R); Merge (L, Mid, Mid+1, R); }}intMain () {Freopen ("Input.txt","R", stdin);  while(Cin >>N) {cnt=0; if(n = =0)             Break;  for(inti =1; I <= N; i++) Cin>>S[i]; MergeSort (1, N); cout<< CNT <<Endl; }    return 0;}

Ultra-quicksort (merge sort, reverse order 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.