Zjnu 1247 merge and sort Reverse Order Pairs

Source: Internet
Author: User
Reverse Order-advanced

Time limit:1000 ms   Memory limit:65536 K
Total submissions:118   Accepted:28

Description

For an array a [1. N] that contains N non-negative integers, If I <j and a [I]> [J], it is called (A [I], a [J]) as a reverse order pair in array.

For example, there are (), () in the reverse order of an array.

Calculate the number of reverse pairs in N.

Input

The first line is an integer N (<= 1,000,000)

N integers in the second row, a [I] <= 2 ^ 31-1

Output

Number of reverse pairs in N count

Sample Input

53 1 4 5 2

Sample output

4. Because the array is too large, this question uses Merge Sorting to find reverse order pairs. Merging and sorting is to divide the array into two portions by means of grouping, and then sort the two portions by combining the two sorted arrays to sort the final arrays. Each time you sort an array, the two sub-arrays of the array are sorted in ascending order, and the two sub-arrays are set to B [I], respectively. c [J] (array B is the first half of the parent array) the parent array is a [K]. The Merged Code is as follows: if (B [I] <= C [J]) A [k ++] = B [I ++]; else a [k ++] = C [J ++]; yi zhi, if B [I]> C [J], in array B, all the values after I are greater than C [J], and the range of array B is [left, mid]. then, the J position of array C can constitute the mid-I + 1 Number of reverse orders. In this way, the total number of reverse orders composed of each element in array C is the total number of reverse orders in array. Therefore, the total number of reverse orders can be calculated during the Merge Sorting Process. The time complexity is the time complexity of the Merge Sorting nlogn. The Code is the code for merging and sorting, but some code for reverse order logarithm is added:
 1 #include <stdio.h> 2 #include <algorithm> 3 #include <string.h> 4 using namespace std; 5  6  7 int a[1000005]; 8 int b[1000005]; 9 __int64 ans;10 11 void bing(int left,int mid,int right)12 {13     int i, j, k;14     for(i=left;i<=right;i++)15     b[i]=a[i];16     i=left;j=mid+1;k=left;17     while(i<=mid&&j<=right)18     {19         if(b[i]<=b[j]) a[k++]=b[i++];20         else{21             a[k++]=b[j++];22             ans+=mid-i+1;23         }24     }25     while(i<=mid) a[k++]=b[i++];26     while(j<=right) a[k++]=b[j++];27 }28 29 void msort(int left,int right)30 {31     if(left<right)32     {33         34         int mid=(left+right)/2;35         msort(left,mid);36         msort(mid+1,right);37         bing(left,mid,right);38     }39 }40 main()41 {42     int n, i;43     scanf("%d",&n);44     for(i=1;i<=n;i++) scanf("%d",&a[i]);45     ans=0;46     msort(1,n);47     printf("%I64d\n",ans);48 }

 

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.