POJ 2299 ultra-quicksort Merge sort to find the inverse number pair

Source: Internet
Author: User

Topic Links:

http://poj.org/problem?id=2299

Title Description:

To a chaotic sequence with n (n<=500000), Q: If you sort the n into ascending order, how many times do you need to exchange it?

Problem Solving Ideas:

According to the characteristics of bubble sort, we know that the problem only need to count each number of reverse order number (if there is i<j, existence a[i] > A[j], then called A[i] and

A[J] for reverse number pairs), the output of all the number of reverse-order number and the normal sort must be timed out, but the relatively fast sort, like fast row and can not be counted

The number of exchanges, here is a good way to reflect the advantages of the merger sort. The typical use of merge sort to find the number of reverse order.

Merge sort: For example now there is a sequence [l,r], we can divide this sequence into two sequences [L,mid], [mid,r], using recursion to follow the

The method is gradually reduced Sequence, the order of the sub-sequence, and then order the sub-sequence interval, and then merge the ordered interval, a good drop embodies the idea of division.

Code:

1#include <cstdio>2#include <cstring>3#include <cstdlib>4#include <iostream>5 using namespacestd;6 #defineMAXN 5000107 8 intA[MAXN], B[MAXN];9 Long Longcount;Ten  One voidMerge (intLintR); A intMain () - { -     intn, I; the      while(SCANF ("%d", &N), N) -     { -Memset (A,0,sizeof(a)); -memset (b,0,sizeof(b)); +          for(i=0; i<n; i++) -scanf ("%d", &a[i]); +Count =0;//must be used int64,int32 will overflow AMerge (0, n); atprintf ("%lld\n", count); -     } -     return 0; - } -  - voidMerge (intLintr)//merge sort, the parameter is the position of the sub-interval respectively in { -     if(R-l <=1) to         return ; +     intMID = L + (r-l)/2; - merge (L, mid); the Merge (Mid, R); *     intx = L, y = Mid, I =l; $      while(X<mid | | y<r)//order the subsequence and save in array bPanax Notoginseng     { -         if(Y >= R | | (X < mid && a[x] <=A[y])) theB[i + +] = a[x + +]; +         Else A         { the             if(X <mid)//record number of exchanges +Count + = mid-x; -B[i + +] = A[y + +]; $         } $     } -      for(i=l; i<r; i++)//Copy the ordered sub-sequence to the corresponding position of the A array -A[i] =B[i]; the}

POJ 2299 ultra-quicksort Merge sort to find the inverse number pair

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.