35数组中的逆序对

 

题目描述

在数组中的两个数字,若是前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数P。并将P对1000000007取模的结果输出。 即输出P%1000000007c++

输入描述:

题目保证输入的数组中没有的相同的数字
数据范围:
对于%50的数据,size<=10^4
对于%75的数据,size<=10^5
对于%100的数据,size<=2*10^5

示例1
数组

1,2,3,4,5,6,7,0

 

输出

7spa

思路:
相似于megersort

先统计子数组内部的逆序对的数目,而后再统计2个相邻子数组之间的逆序对的数目,在统计的过程当中,还须要对数组排序,
排序能够避免重复统计。
code

 1 public class Solution {
 2     public int InversePairs(int [] a) {  
 3         int[] aux = new int[a.length];
 4         for(int i = 0;i<a.length;i++)
 5             aux[i] = a[i];
 6         
 7         return fun(a,aux,0,a.length-1);
 8     }
 9     private int fun(int[] a,int[] aux,int lo,int hi){
10         if(lo==hi) return 0;
11         int mid = (hi-lo)/2+lo;
12         int left = fun(a,aux,lo,mid)%1000000007;
13         int right = fun(a,aux,mid+1,hi)%1000000007;
14         
15         int i = mid;
16         int j = hi;
17         int ai = hi;//aux_index
18         int  count = 0;
19         while(i>=lo&&j>=mid+1){
20             if(a[i]>a[j]){
21                 aux[ai--] = a[i--];
22                 count+=j-mid;
23                 if(count>=1000000007)//数值过大求余
24                     count%=1000000007;       
25             }
26             else
27                 aux[ai--]=a[j--];
28         }
29         
30         while(i>=lo)
31             aux[ai--]=a[i--];
32         while(j>=mid+1)
33             aux[ai--] =a[j--];    
34         
35         //更新数组
36         for(int k = lo;k<=hi;k++)
37             a[k] = aux[k];
38         
39         return (left+right+count)%1000000007;
40     }
41 }

 

 

 

c++ 20180725blog

 

 1 class Solution {
 2 public:
 3     int InversePairs(vector<int> data) {
 4         int length = data.size();
 5         if(length<=0) return 0;
 6         std::vector<int> copy(data);
 7         long long cnt = InversePairsCore(data,copy,0,length-1);
 8         return cnt;
 9     }
10     long long InversePairsCore(std::vector<int> &data,std::vector<int> &copy,int lo,int hi){
11         if(lo==hi) return 0;
12         int mid = lo+(hi-lo)/2;
13         long long left = InversePairsCore(data,copy,lo,mid);
14         long long right  =InversePairsCore(data,copy,mid+1,hi);
15 
16         int i = mid;
17         int j = hi;
18         int k = hi;
19         int cnt = 0;
20         while(i>=lo&&j>=mid+1){
21             if(data[i]>data[j]){
22                 copy[k--]=data[i--];
23                 cnt+=j-mid;
24                 if(cnt>=1000000007)
25                 cnt%=1000000007;
26             }
27             else{
28                 copy[k--]=data[j--];
29             }
30         }
31         while(i>=lo)
32             copy[k--] = data[i--];
33         while(j>=mid+1)
34             copy[k--] = data[j--];
35         for(int m = lo;m<=hi;m++)
36             data[m]  =copy[m];
37         return (left+right+cnt)%1000000007;
38     }
39 };
相关文章
相关标签/搜索