Poj2299 merge sort to calculate the number of reverse orders

Source: Internet
Author: User

The question is very simple. Find the number of exchanges in the worst case of quicksort. After understanding it, we know that it should be a reverse-order pair problem of the sequence.

It is found that the nlog (n) sorting speed of Merge Sorting is very good, while in the sorting process, there is a merge process Merge (), here we will merge two ordered series into an ordered series. In the process of merging, it is easy to calculate the number of Reverse Order pairs, so this problem can be quickly solved.

That is, for the merge of the series [L, mid] [Mid + 1, R], I starts from L and J starts from Mid + 1, if a [I]> A [J] is displayed in reverse order, you can add a [J] to the secondary array and J ++, then, the number in the descending order of a [J] has a mid-I + 1, because the sequence is an ordered [I, mid] and all the numbers are greater than a [J.CodeAs follows. I have relived the writing and thought of merging ..

Code

  1   # Include  <  Stdio. h  >  
2 # Include < String . H >
3 # Include < String . H >
4 # Include < Stdlib. h >
5 # Include < Math. h >
6
7 # Include < Iostream >
8   Using Namespace STD;
9
10 # Define Nn 500004
11 # Define NL 1000.
12
13 _ Int64 res;
14 Int B [NN]; // Intermediate auxiliary Array
15
16 Void Copy ( Int A [], Int L, Int R ){
17 Int I;
18 For (I = L; I <= R; I ++ ){
19 A [I] = B [I];
20 }
21 }
22 Void Merge ( Int A [], Int L, Int Mid, Int R ){
23 Int I = L;
24 Int J = Mid + 1 ;
25 Int K = L;
26 While (I <= Mid && J <= R ){
27 If (A [I] < A [J]) {
28 B [K ++ ] = A [I];
29 I ++ ;
30 } Else {
31 B [K ++ ] = A [J];
32 J ++ ;
33 Res + = Mid - I + 1 ;
34 }
35 }
36 While (I <= Mid ){
37 B [K ++ ] = A [I];
38 I ++ ;
39 }
40 While (J <= R ){
41 B [K ++ ] = A [J];
42 J ++ ;
43 }
44 }
45
46 Void Mergesort ( Int A [], Int L, Int R ){
47 // Merge Sorting
48 If (L < R ){
49 Int Mid = (L + R) > 1 ;
50 Mergesort (A, L, mid );
51 Mergesort (A, mid + 1 , R );
52 Merge (A, L, mid, R );
53 Copy (A, L, R );
54 }
55 }
56 Int Main (){
57 Int N, I;
58 Int F [NN];
59 While (Scanf ( " % D " , & N) ! = EOF ){
60 If (N = 0 ) Break ;
61 For (I = 1 ; I <= N; I ++ ){
62 Scanf ( " % D " , & F [I]);
63 }
64 Res = 0 ;
65 Mergesort (F, 1 , N );
66 Printf ( " % I64d \ n " , Res );
67 }
68 Return 0 ;
69 }
70

 

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.