Ultra-quicksort (array sorting problem)

Source: Internet
Author: User

Title Link: Http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/A

Test instructions

Several groups of cases, first enter the number of data, in order to enter the data, to determine the minimum number of these data to be arranged in the amount of operation steps. (case with input 0 end)

Case:

Sample Input

5

9

1

0

5

4

3

1

2

3

0

Sample Output

6

0
Analysis:

This question I refer to a great God's thinking analysis, I think the explanation is very detailed, but also gave me the idea of thinking, so borrowed. Specific as follows:

The nature of the topic is to seek reverse order, a brief introduction. Reverse pair refers to the sequence {a0,a1,a2...an}, if Ai<aj (I>j), then (Ai,aj) a pair of reverse pairs. And the inverse number is the name of the sequence of the number of reverse pairs. For example: 1 2 3 is the order, then the reverse number is 0;1 3 2 (2,3) to meet the reverse of the condition, so the reverse number is only 1; 3 2 1 () (1,3) (2,3) satisfies the reverse pair, so reverse is 3. By definition it cannot be imagined that the inverse number of sequence n is in the range of [0,n* (n-1)/2], where reverse order is 0, and reverse is n (n-1)/2.

can be used to calculate the number of reverse order when merging, time complexity is nlog2n, and spatial complexity of 2n. The use of merge to reverse order refers to the sub-sequence S1 and S2 at the time of merging, if S1[I]>S2[J] (reverse order condition), then the reverse number plus s1.length-i, because the S1 in the back of the number for S2[J] are reversed. Look at the notes for details.

The basic idea of merging algorithm

Set two ordered sub-files in the same vector adjacent to the position: R[low. M],r[m+1..high], merge them into a local staging vector R1, and copy R1 back to R[low when the merge is complete. High].

During the merge process, the I,J and p three pointers are set, and their initial values point to the starting position of the three record areas respectively. When merging, compare R[i] and r[j] keywords, take a smaller record of the keyword to r1[p], and then copy the recorded pointer I or J plus 1, and pointer to the copy position p plus 1.

Repeat this process until one of the two input sub-files has been completely copied (it may be called empty), and then the remaining records in the other non-empty sub-file are copied sequentially to R1.

Source:

1#include <cstdio>2 #defineMAX 5000013 intA[max], b[max],i;4 Long Longans;5 voidMergeintLintMintR//Merge6 {7     intp=0, i=l,j=m+1;//p point to output, i,j point to input8      while(I&LT;=M&AMP;&AMP;J&LT;=R)//Two input space is not 09     {Ten         if(A[i]>a[j])//take the minimum value into the output space One         { Ab[p++]=a[j++]; -ans+=m-i+1;//A[i] In reverse order of values relative to A[i] -         } the         Else -b[p++]=a[i++]; -     } -      while(i<=m) b[p++]=a[i++];//non-empty input space into output space +      for(i=0; i<p;++i)//merge End, result copied to original input array -a[l+i]=B[i]; + } A voidMergeSort (intL2,intR2)//Merge Sort at { -     if(l2<R2) -     { -         intM2= (L2+R2)/2;//divides a sequence that is long n into two parts -MergeSort (L2,M2);//ordering a sequence of pairs -MergeSort (m2+1, r2); inMerge (L2,M2,R2);//merging sub-sequences -     } to } + intMain () - { the     intN; *      while(SCANF ("%d", &n) &&N) $     {Panax Notoginsengans=0; -          for(i=0; i<n;i++) thescanf"%d",&a[i]); +MergeSort (0, N-1); Aprintf"%lld\n", ans); the     } +     return 0; -}

Ultra-quicksort (array sorting problem)

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.