Hiho, 39th week. Merge sort for reverse order number

Source: Internet
Author: User
Tags cmath

 Topic Link: http://hihocoder.com/contest/hiho39/problem/1, merge sort to seek reverse number.

In fact, this problem can be done with a tree-like array, but the data are relatively large, so to be discretized preprocessing, the text will also give the discretization + Tree array solution, but more slowly than the merge sort.

Algorithm:  

or according to the solution given in the question.

Let's take a look at a merge sort process:
The given array is [2, 4, 5, 3, 1], the binary array is [2, 4, 5], [1, 3], assuming we have completed the sub-procedure, and now proceed to the "and" Operation of the array:

A: [2, 4, 5] B: [1, 3] result:[1] Select the B array 1
A: [2, 4, 5] B: [3] result:[1, 2] Select the 2
A: [4, 5] B: [3] result:[1, 2, 3] Select 3 of the B array
A: [4, 5] b: [] result:[1, 2, 3, 4] Select 4 of the A array
A: [5] b: [] result:[1, 2, 3, 4, 5] Select 5 of the A array

When we merge [2, 4, 5] and [1, 3] we can find that when we put the element k of a array into the result array, the elements of the B array that exist in result must be smaller than K. In the original array, the position of the elements in the B array must be after K, that is, k and these elements constitute the inverse pair. So when we put the elements in the a array, we can calculate the number of pairs in the B array for k, by calculating the number of elements in the B array in result.

And because of the recursive process, the a array and K satisfy the number of reverse pairs is also calculated. At the end of the recursion, the number of all k in the reverse order of [2, 4, 5, 3, 1] is counted. The same is true for the other elements in a. Since each merging process has the same situation, we can easily infer that:

The total number of reversed pairs in the original array can be obtained if the number of reverse orders obtained in each merge process is added together.

  That is, the number of reverse pairs is calculated in a single merge order, and the time complexity is O (NLOGN).

#include <iostream>#include<cstdio>#include<vector>#include<queue>#include<cmath>#include<string>#include<string.h>#include<algorithm>using namespacestd;#defineLL Long Long#defineEPS 1e-8#defineINF 1000005Const intMAXN =100000+5;intA[MAXN], B[MAXN]; LL sum;voidMergeintA[],intB[],intLintMintR) {    inti = l, j = m +1, k =0; intCNT =0;  while(I <= m && J <=r) {if(A[i] <=A[j]) {B[k+ +] = a[i++]; Sum+=CNT; } Else{b[k+ +] = a[j++]; CNT++; }    }     while(I <=m) {b[k+ +] = a[i++]; Sum+=CNT; }     while(J <=r) B[k+ +] = a[j++];  for(inti =0; I < K; i++) A[i+ L] =b[i];}voidMerge_sort (intA[],intI IintR) {    if(L <r) {intm = (L + r) >>1;        Merge_sort (A, L, M); Merge_sort (A, M+1, R);    Merge (A, B, L, M, R); }}intMain () {intN; CIN>>N;  for(inti =0; I < n; i++) scanf ("%d", &A[i]); Sum=0; Merge_sort (A,0N1); cout<< sum <<Endl; return 0;}
Merge

You can also scatter + tree arrays:

The data is stored, then sorted, so that the original number in the sorted array after the subscript can be used as a new value, so as to disperse the data.

The method of finding the inverse number of the tree array: Suppose to find the inverse of the array a[], in reverse order to insert each element in the array into the tree array a[i] corresponding to the position, when inserting each element, the count is smaller than the number of elements. After a single traversal, you can get all the reverse numbers.

#include <iostream>#include<cstdio>#include<cmath>#include<queue>#include<vector>#include<string>#include<string.h>#include<algorithm>using namespacestd;Const intMAXN =100000+5;intA[MAXN], C[MAXN], N;intLowbit (intx) {    returnX & (-x);}voidUpdateintXintnum) {     while(x <=N) {c[x]+=num; X+=lowbit (x); }}intGetsum (inti) {    intres =0;  while(I >0) {res+=C[i]; I-=lowbit (i); }    returnRes;}intBinary_search (intA[],intLintRintx) {    intm = (L + r) >>1;  while(L <=r) {if(A[m] = =x)returnm; if(A[m] <x) L= m +1; if(A[m] >x) R= M-1; M= (L + r) >>1; }    return-1;}intMain () {CIN>>N;  for(inti =1; I <= N; i++) {scanf ("%d", &A[i]); C[i]=A[i]; } sort (c+1, C + n +1);  for(inti =1; I <= N; i++) {        intj = Binary_search (c,1, N, A[i]); A[i]=J; } memset (c,0,sizeof(c)); Long Longsum =0;  for(inti = n; I >=1; i--) {sum+ = Getsum (A[i]-1); Update (A[i],1); } cout<< sum <<Endl; return 0;}
tree-like array

  

Hiho, 39th week. Merge sort for 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.