Topic: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%1000000007Input 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 input
1,2,3,4,5,6,7,0
Output
7
Analysis of ideas (sword Point offer): Seeing this topic, our first reaction is to sequentially scan the entire array. When you do not scan an array, compare the size of the number and the number behind it. If the subsequent number is smaller than it, then these two numbers make up an inverse pair. Suppose the array contains n digits. Since each number is compared to the number O (n), the time complexity of the algorithm is O (n^2). We use the array {7,5,6,4} as an example to analyze the process of counting reverse pairs. Each time we scan to a number, we do not compare the TA with each of the following numbers, otherwise the time complexity is O (n^2), so we can consider the first comparison of two adjacent numbers. , &NB Sp (a) decompose an array of length 4 into two sub-arrays of length 2, and (b) decompose an array of length 2 into two sub-arrays of Chengdu 1; (c) The sub-array of length 1 merge, sort, and statistics in reverse order pairs ; (d) merges, sorts, and counts the sub-arrays of length 2 in reverse order to; in (a) and (b), We first decompose the array into two sub-arrays of length 2, and then split the two sub-arrays into two sub-arrays of length 1. The next side merges adjacent sub-arrays, counting the number of reverse pairs. In the first pair of sub-arrays of length 1 {7}, {5}, 7 is greater than 5, so (7,5) constitutes an inverse pair. Also there are reverse pairs (6,4) in the second pair of sub-arrays {6}, {4}, which have a length of 1. Since we have counted the inverse pairs inside the pairs array, we need to, the two pairs array sort as shown in (c) to avoid repeating the statistics in the subsequent statistical process. Next we count the inverse pairs between two sub-arrays with a length of 2. Merge the sub-arrays and count the reverse pairs as shown in the procedure. We first point to the end of two sub-arrays with two pointers, and compare the numbers pointed to by two pointers at a time. If the firstA number in a sub-array is larger than the number in the second array, which is the inverse pair, and the number of reversed pairs equals the number of digits remaining in the second Subarray, as shown in (a) and (c). If the number of the first array is less than or equal to the number in the second array, it does not constitute an inverse pair, as shown in B. Each time we compare, we copy the larger number forward from the back to a secondary array, making sure that the numbers in the auxiliary array (recorded as copy) are ascending sorted. After the larger number is copied to the secondary array, move the corresponding pointer forward one bit, then the next round comparison. Process Summary:
Divide the array into sub-arrays, count the number of reverse pairs within the subarray, and then count the number of inverse pairs between the two adjacent sub-arrays.
Reference code:
Public classInversepairs {Private intCount = 0; Public intInversepairs (int[] a) {if(A = =NULL|| A.length = = 0) return-1; MergeSort (A,0, A.length-1); returncount; } Public voidMergeSort (int[] A,intLeftintRight ) { if(Left <Right ) { intMid = (left + right)/2; MergeSort (A, left, mid); MergeSort (A, Mid+ 1, right); Merge (A, left, Mid, right); } } Public voidMergeint[] A,intLeftintMidintRight ) { int[] tmp =New int[Right-left + 1]; intT = right-left;//Temporary Array subscriptintL =mid; intR =Right ; while(l >= left && R >= mid + 1) { if(A[l] >A[r]) {Count+ = (R-mid); Tmp[t--] = a[l--]; if(Count >= 1000000007) {Count%= 1000000007; } } Else{tmp[t--] = a[r--]; } } while(L >=Left ) {tmp[t--] = a[l--]; } while(R >= Mid + 1) {tmp[t--] = a[r--]; } for(inti = 0; I <= right-left; i++) {A[left+ i] =Tmp[i]; }} @Test Public voidtestsolution () {int[] A = {364, 637, 341, 406, 747, 995, 234, 971, 571, 219, 993, 407, 416, 366, 315, 301, 601, 650, 418, 355, 460, 505, 360, 965, 516, 648, 727, 667, 465, 849, 455, 181, 486, 149, 588, 233, 144, 174, 557, 67, 746, 550, 474, 162, 268, 142, 463, 221, 882, 576, 604, 739, 288, 569, 256, 936, 275, 401, 497, 82, 935, 983, 583, 523, 697, 478, 147, 795, 380, 973, 958, 115, 773, 870, 259, 655, 446, 863, 735, 784, 3, 671, 433, 630, 425, 930, 64, 266, 235, 187, 284, 665, 874, 80, 45, 848, 38, 811, 267, 575 }; System.out.println (Inversepairs (a));//2519}}
The reverse order in the array