Sword refers to the reverse order in the array of offer 35

Source: Internet
Author: User

First, the 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%1000000007

Second, the idea

Method One: 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). Method Two: We use the array {7,5,6,4} as an example to analyze the process of statistical reverse order 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. 1, 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 put these two pairs of arrays Sort,As shown in (c), avoid repeating the statistics in the subsequent statistical process. 2. Next we count two sub-arrays with a length of 2 in reverse order.          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 number in the first subarray is greater than the number in the second array, the inverse pair is formed, 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 numbers from the back to a secondary array, ensuring auxiliary Array (recorded as copy)The numbers in are ascending sort. After the larger number is copied to the secondary array, move the corresponding pointer forward one bit, then the next round comparison. The detailed procedure in the corresponding diagram is as follows: (a) The number pointed to by P1 is greater than the number pointed to by P2, indicating that there is an inverse pair in the array. The number that P2 points to is the second number of the second Subarray, so the second sub-array has two digits smaller than 7. Add 2 to the number of reverse order, and copy 7 to the auxiliary array, moving forward P1 and P3.
(b) P1 points to the number P2 pointing to the number, no reverse order. Copy the number that P2 points to the secondary array and move forward P2 and P3.
(c) The number pointed to by P1 is greater than the number pointed to by P2, so there is an inverse pair. Since the number pointed to by P2 is the first number of the second subarray, only one digit in the sub-array is smaller than 5.  Add 1 to the number of reverse order, and copy 5 to the auxiliary array, moving forward P1 and P3. (d) Copy the last remaining 4 of the second subarray into the secondary array. 3, after the detailed theory of poetics, we can summarize the process of statistical reverse order: First, the array is divided into sub-arrays, the number of inverse pairs of the sub-array is counted, and then the number of reverse pairs between the two adjacent sub-arrays is counted. In the process of counting reverse pairs, you also need to sort the arrays. If you are familiar with sorting algorithms, it is not difficult to see that this process is actually a merge sort.

Third, the Code

/*Merge sorting improvement, the data is divided into two arrays (recursive to each array only one data item), merging arrays, when merging, the preceding array value array[i] is greater than the following array value array[j]; The preceding array array[i]~array[mid] are larger than Array[j], count + = mid+1-i Reference sword refers to offer, but feel the sword refers to the offer merge process less one-step copy process. And the test case output is relatively large, for each return of the Count mod (1000000007) to find redundancy*/Public class Solution { publicintInversepairs (int[] Array) {        if(Array = =NULL|| Array.Length = = 0) {            return0; }        //Defining auxiliary Arrays        int[] copy =New int[Array.Length];  for(inti = 0; i < Array.Length; i++) {Copy[i]=Array[i]; }        //The value is too large to seek redundancy        intCount = Inversepairscore (array, copy, 0, array.length-1); //return Results        returncount; } PrivateintInversepairscore (int[] Array,int[] Copy,intLowintHigh ) {        if(Low = =High ) {            return0; }        intMid = (low + high) >> 1; intLeftcount = Inversepairscore (array, copy, Low, mid)% 1000000007; intRightcount = Inversepairscore (array, copy, Mid + 1, high)% 1000000007; intCount = 0; inti =mid; intj =High ; intLoccopy =High ;  while(I >= low && j >mid) {if(Array[i] >Array[j]) {Count+ = J-mid; Copy[loccopy--] = array[i--]; if(Count >= 1000000007)//The value is too large to seek redundancy{Count%= 1000000007; }            } Else{copy[loccopy--] = array[j--]; }        }         for(; I >= low; i--) {copy[loccopy--] =Array[i]; }         for(; j > mid; j--) {copy[loccopy--] =Array[j]; }         for(ints = low; s <= high; s++) {Array[s]=Copy[s]; }        return(Leftcount + rightcount + count)% 1000000007; }}
View Code

---------------------------------------------

Reference Links:

Https://www.nowcoder.com/questionTerminal/96bd6684e04a44eb80e6a68efc0ec6c5

Sword refers to the reverse order in the array of offer 35

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.