Prove safety offer35: reverse in the array

1 Title Description

  Two numbers in the array, if a number is greater than the front behind the figures, the two numbers form a reverse pair. Enter an array, this array P. finds the total number of reverse pair P and outputs the result modulo 1000000007. I.e., the output P% 1000000007

2 ideas and methods

  Thought the use of merge sort, the first array partitioned into sub-arrays, the first count of the number of reverse sub-array inside, then the statistics of the number of reverse between two adjacent sub-arrays. Note After you merge two sorted sub-array, the array to be updated. O (n * log (n)).

 

3 C ++ core code

 1 class Solution {
 2 public:
 3     int InversePairs(vector<int> data) {
 4         if(data.size() <= 1)
 5             return 0;
 6         int count = 0;
 7         vector<int> copy(data); // 初始化
 8         InversePairsCore(data, copy, 0, data.size()-1,count);
 9         return count;
10     }
11 
12     //Merge 
13 is      void InversePairsCore (Vector < int > & Data, Vector < int > & Copy, int Start, int End, int & COUNT) {
 14          IF (Start> = End) {
 15              return ;
 16          }
 . 17          int MID = (Start + End ) / 2 ;
 18 is          InversePairsCore (Data, Copy, Start, MID, COUNT);
 . 19          InversePairsCore (Data, Copy, MID + . 1 , End, COUNT);
 20 is  
21 is          int copyIndex = End; // larger numbers from the back copied to the secondary array
22 is          int I = MID;     // The last element of the first half of the standard 
23 is          int J = End;     // half of the last element under subscript 
24          the while (I> = Start && J> = MID + . 1 ) {
 25              IF (Data [J] <Data [I]) {      // reverse 
26 is                  COUNT = J + - MID;
 27                  COUNT% = 1,000,000,007 ;     // modulo prevent reverse overflow 
28                  Copy [copyIndex--] = Data [i-- ];
 29              } the else {
 30                  Copy [copyIndex--] = Data [J, ];
31             }
32         }
33 
34         while (i>=start)
35             copy[copyIndex--] = data[i--];
36 
37         while (j>=mid+1)
38             copy[copyIndex--] = data[j--];
39 
40         for (int k = start; k <= end; ++k)
41             data[k] = copy[k];
42 
43     }
44 };
View Code

4 C ++ complete code

. 1 #include <the iostream>
 2  the using  namespace STD;
 . 3  // array in reverse order 
. 4  Long  Long GetMergePairsBetween ( int * ARR, int * Copy, int Start, int MID, int End)
 . 5  {
 . 6      // merge two sub-arrays , and calculates the number of reverse order 
. 7      int Final1 = MID; // first the last bit of the array 
. 8      int Final2 = End; // the last one of the second array 
. 9      int index = End; // auxiliary array last 
10      Long  Long= COUNT 0 ;
 . 11      the while (Final1> = Start && Final2> + = MID . 1 ) // two arrays are not processed 
12 is      {
 13 is         IF (ARR [Final1]> ARR [Final2])
 14         {
 15             // If the first elements of the second array is greater than any of the elements of the array,
 16             // the first element of the array must be greater than all the elements of the array before the Final2 
. 17             COUNT + = (Final2 - MID);
 18 is             // the at final1 copy to copy the array elements
 . 19             // index and are moved forward final1 
20 is             copy [index the] = ARR [final1-- ];
 21 is         }
 22 is         the else
23         {
 24             // element array is less than the first element of the second array
 25             // second element of the array is copied to the copy array
 26             @ and the forward index and final2 
27             copy [index The] = ARR [final2-- ];
 28         }
 29      }
 30      the while (Final1> = Start) // element of the array is not a processed 
31 is      {
 32         Copy [index the] = ARR [final1-- ];
 33 is      }
 34 is      the while (Final2> + = MID . 1 ) // element of the array is not processed a 
35      {
 36         Copy [index the] = ARR [final2--];
37     }
38     for(int i = end; i > index;i--) 
39         arr[i] = copy[i];
40     return count;
41 }
42 long long GetMergePairs(int* arr,int* copy,int start,int end)
43 {
44     long long ret = 0;
45     if(start < end)
46     {
47        int mid = start + ((end - start)>>1);
48         year + = GetMergePairs (arr, copy, start, mid);
49         year + = GetMergePairs (arr, copy, mid + 1 ; End);
50         year + = GetMergePairsBetween (arr, copy, start, mid, end);
51      }
 52      return ret;
53  }
 54  Long  Long GetTotalPairs ( int arr [], int n)
 55  {
 56      if (arr == null || n < 2 )
 57         return  0 ;
58      int * copy = new  int[n];
59      Long  Long sum = GetMergePairs (arr, copy, 0 , n- 1 );
60      delete [] copy;
61      return sum;
62  }
 63  int main ()
 64  {
 65      int arr [] = { 7 , 5 , 6 , 4 };
66      int ret = GetTotalPairs (arr, sizeof (ARR) / sizeof (arr [ 0 ]));
67      cout << ret << endl;
68      system ( "pause");
69     return 0;
70 }
View Code

Reference material

https://blog.csdn.net/zjwreal/article/details/88769617

https://blog.csdn.net/DERRANTCM/article/details/46761051 (FIG)

https://blog.csdn.net/peiyao456/article/details/54645952 (complete code)

 

Guess you like

Origin www.cnblogs.com/wxwhnu/p/11421187.html