Merge sort method to find the inverse ordinal

Original address: https://blog.csdn.net/ameir_yang/article/details/53575595

Problem Description

For any two numbers ai,aj (i < j) in the sequence a1,a2,a3..., if ai > aj, then we say that these two numbers constitute an inversion pair; the total number of inversion pairs in a sequence Call it an inverse number, for example, in the sequence 1 6 3 7 2 4 9, (6,4) is an inverse pair, and there are also (3,2), (7,4), (6,2), (6 ,3) Wait, your task is to find the inverse number of the sequence for the given sequence.

Input

The input data N (N <= 100000) represents the number of elements in the sequence, and then input N positive integers with spaces between the numbers.

 

Output

Output the reverse order number.

Example Input

10
10 9 8 7 6 5 4 3 2 1

Example Output

45

Hint

Personal understanding is like this, so easy to understand.


Merge Sort:  Means from local to global. Sort all the parts first, and then combine the parts one by one into a whole. Because every part is ordered, then the whole is also ordered. It can also be understood in this way: the parts are arranged, and then the whole is arranged. It's like a sequence: split it down the middle, order the first half first, then the second half, and then the whole order. This process is implemented layer by layer using recursive calls. The reason for using recursion is: recursion means to search from the top all the way down, and when you find the bottom, you can't find any more and stop recursion. That is to say, looking down from the whole, all the way down, dividing the whole into small individuals, and in the end, there is no way to divide it, which means that it is the easiest. Sort from the simplest, and then look up, slowly increase the size of the individual, and sort the individual in the process of increasing. Finally, the top is the whole, and finally the whole is sorted; 


====================================================================================

代码中递归(Merge_sor函数t)那几步是如何与Merge函数相互连接的:                                    

首先:[整体]----->[个体1]---->[个体2] ----->[个体3] ---->[最简单体]  先往下面找;

然后  [最简单体排序] ---->[个体3排序] ----->[个体2排序]-----> [个体1排序]--->[整体排序] ;

====================================================================================


以整体为例:

对该序列:7 ,10 , 19 , 25 , 12 ,17,21,30,48 ;进行排序; 


以下为处理前的步骤:

第一步:


第二步:





以下为归并排序步骤:


结果:



代码如下:

[cpp]  view plain  copy
  1. <span style="font-size:14px;">#include<stdio.h>  
  2. #include<stdlib.h>  
  3.   
  4. long long sum ;  
  5. int a[100010] ; //存放元素;  
  6. int temp[100010] ; //存放通过归并排序排序后的元素;  
  7.   
  8.   
  9. //往temp中放入元素,并且实时更新a数组中的元素顺序,把通过用归并排序排好的元素先放进temp中,然后再用temp给a赋值,这样对a数组进行更新;  
  10. void Merge(int s1,int e1,int s2,int e2)  
  11. {  
  12.     int p = 0 ;  
  13.     int p1 = s1 ;  
  14.     int p2 = s2 ;  
  15.     while(p1<=e1&&p2<=e2)  
  16.     {  
  17.         if(a[p1]<=a[p2])  
  18.         {  
  19.             temp[p++] = a[p1++] ;  
  20.         }  
  21.         else  
  22.         {  
  23.             temp[p++] = a[p2++] ;  
  24.             sum+=(e1-p1+1); //这里不懂的,就这样写就行;因为是这个题的缘故。真正的归并排序算法中国m,没有这个得。  
  25.         }  
  26.     }  
  27.     //如果mid前面的元素数目比mid后面的元素数目少,那么后面就剩下了很多的元素;  
  28.     while(p1<=e1)  
  29.     {  
  30.         temp[p++] = a[p1++] ;  //这里就是为了把后面余下的mid前面元素放进temp中;  
  31.     }  
  32.     while(p2<=e2)  
  33.     {  
  34.         temp[p++] = a[p2++] ; //这里就是为了把后面余下的mid后面元素放进temp中;  
  35.     }  
  36.     int i ;  
  37.     for(i=s1;i<=e2;i++)  
  38.     {  
  39.         a[i] = temp[i-s1] ;  //用temp数组对a数组进行更新;  
  40.     }  
  41. }  
  42.   
  43.   
  44. void Merge_sort(int s ,int e)  
  45. {  
  46.     int mid ;  
  47.     if(s<e)  
  48.     {  
  49.         mid = (s+e)/ 2 ;  
  50.         Merge_sort(s,mid); //左边递归到最小;  
  51.         Merge_sort(mid+1,e);//右边递归到最小;  
  52.         Merge(s,mid,mid+1,e) ;  
  53.     }  
  54. }  
  55.   
  56.   
  57. int main()  
  58. {  
  59.     int n ;  
  60.     scanf("%d",&n); //输入的元素个数;  
  61.     int i ;  
  62.     for(i=0;i<n;i++)  
  63.     {  
  64.         scanf("%d",&a[i]); //输入的元素数值;  
  65.     }  
  66.     sum = 0 ;  
  67.     Merge_sort(0,n-1) ; //调用归并排序函数;  
  68.     printf("%lld\n",sum);  
  69.     return 0 ;  
  70. }  
  71. </span>  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324441221&siteId=291194637