Title Description
In the array of two digits, if the previous number is greater than the number that follows, then these two numbers form an inverse pair. Enter an array to find the total number of reverse pairs in this array p. and the output of p to 1000000007 modulo is obtained. i.e. Output p%1000000007
Input Description:
The title guarantees that the input array does not have the same number
Data range:
For%50 data, size<=10^4
For%75 data, size<=10^5
For%100 data, size<=2*10^5
Example 1
Lose
1,2,3,4,5,6,7,0
Output
7
Ideas:
Similar to Megersort
Counting the number of inverse pairs inside the subarray, and then counting the number of reverse pairs between the 2 adjacent sub-arrays, in the process of statistics, we also need to sort the array,
Sorting can avoid repeating statistics.
1 Public classSolution {2 Public intInversepairs (int[] a) {3 int[] aux =New int[a.length];4 for(inti = 0;i<a.length;i++)5Aux[i] =A[i];6 7 returnFun (a,aux,0,a.length-1);8 }9 Private intFunint[] A,int[] aux,intLointhi) {Ten if(Lo==hi)return0; One intMid = (Hi-lo)/2+Lo; A intleft = Fun (A,aux,lo,mid)%1000000007; - intright = Fun (A,aux,mid+1,hi)%1000000007; - the inti =mid; - intj =Hi; - intai = Hi;//Aux_index - intCount = 0; + while(i>=lo&&j>=mid+1){ - if(a[i]>A[j]) { +aux[ai--] = a[i--]; Acount+=j-mid; at if(count>=1000000007)//The value is too large to seek redundancy -count%=1000000007; - } - Else -aux[ai--]=a[j--]; - } in - while(i>=lo) toaux[ai--]=a[i--]; + while(j>=mid+1) -aux[ai--] =a[j--]; the * //updating an array $ for(intK = lo;k<=hi;k++)Panax NotoginsengA[K] =Aux[k]; - the return(Left+right+count)%1000000007; + } A}
35 reverse order in the array