Several methods of Reverse Order

Source: Internet
Author: User
Tags cmath

Several methods of Reverse Order

The number of reverse orders is A number of reverse orders, for example, if array A = {2, 4, 3, 5}, <4, 3>.

I. brute force matching

For elements in array A, I ranges from 0 to n-1, and j ranges from I + 1 to n to determine whether each element is A number in reverse order and the time complexity is O (lgN ). It's too simple to write code .....

Ii. Merge

Merge Sorting can solve the reverse order mainly in: for example, when merging A1 = {2, 4, 5}, A2 = {1, 3}, every time we judge the element size in A1 and A2, there are two ideas: (1) When A1 [I] <= A2 [j], you need to put A1 [I, we can calculate the number of elements in A2 that has been put into the j-mid-1; thought (2) When A1 [I] <= A2 [j], we can put it directly, however, when A1 [I]> A2 [j], we need to put A2 [j] to calculate the number of elements in A1, the number of reverse orders corresponding to them A2 [j. The idea is that the merge algorithm needs to be modified in two ways. The idea 2 only needs to be modified in one way. The time complexity is O (lgN)

Idea (1) code:

 

Void mergeSortInverseNumber (int a [], int p, int q, int a1 [], long int & number) {if (p <q) {int r = (p + q)/2; mergeSortInverseNumber (a, p, r, a1, number); mergeSortInverseNumber (a, r + 1, q, a1, number ); int I = p, j = r + 1; int k = p; while (I <= r & j <= q) {if (a [I] <= a [j]) {a1 [k ++] = a [I ++]; number ++ = j-r-1; // when a [I] is put in, the number of existing seq2 values is the number of reverse orders} elsea1 [k ++] = a [j ++];} if (I> r) {while (j <= q) a1 [k ++] = a [j ++];} else {while (I <= r) {a1 [k ++] = a [I ++]; number + = j-r-1; /// here you also need to modify} for (int I = p; I <= q; I ++) a [I] = a1 [I] ;}}

 

Idea (2) code:

 

Void mergeSortInverseNumber2 (int a [], int p, int q, int a1 [], long int & number) {if (p <q) {int r = (p + q)/2; mergeSortInverseNumber (a, p, r, a1, number); mergeSortInverseNumber (a, r + 1, q, a1, number ); int I = p, j = r + 1; int k = p; while (I <= r & j <= q) {if (a [I] <= a [j]) a1 [k ++] = a [I ++]; else {a1 [k ++] = a [j ++]; number + = r-I + 1; // when a [j] is put, the number of elements in seq1 is not put in the reverse order.} if (I> r) {while (j <= q) a1 [k ++] = a [j ++];} else {while (I <= r) a1 [k ++] = a [I ++];} for (int I = p; I <= q; I ++) a [I] = a1 [I];}

 

3. Tree Array

The tree array solution is hard to understand, and I do not need to understand it. Here is an online code:

 

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
Using namespace std; const int maxn = 500005; int n; int aa [maxn]; // The int c [maxn] array after discretization; // tree array struct Node {int v; int order;} in [maxn]; int lowbit (int x) {return x & (-x );} void update (int t, int value) {int I; for (I = t; I <= n; I + = lowbit (I )) {c [I] + = value ;}} int getsum (int x) {int I; int temp = 0; for (I = x; I> = 1; i-= lowbit (I) {temp + = c [I];} return temp;} bool cmp (Node a, Node B) {return. v
     
      

 

Since we know from the previous blog that all the problems that can be solved by tree Arrays can be solved by line tree, why not use line tree?

 

4. Line Segment tree

The idea of solving the number of reverse orders in the line segment tree is to set the array size to N, and the elements are also 1 ~ Then we can create a line segment tree [1, N. When traversing each element x in the array, We need to query the number of inserted elements between [x + 1, N] In the Line Segment tree, and update the line segment tree to set the interval [x, x] And all intervals containing x plus 1. Create a line segment tree as O (N) and obtain the reverse order number as O (lgN ).

Of course, the premise here is that the element size is 1 ~ If N is not in this range, we can compress discrete data. See the following code:

 

// Compress the discrete data into struct arrayExtend {int val; int index ;};
Int N; cin> N; int * a = new int [N + 1]; // compress and compress data using a struct in the range of 1 ~ The order between N remains unchanged. arrayExtend * a1 = new arrayExtend [N + 1]; for (int I = 1; I <= N; I ++) {cin> a1 [I]. val; a1 [I]. index = I;} sort (a1 + 1, a1 + N + 1, cmp); // sort a [a1 [1]. index] = 1; for (int I = 2; I <= N; I ++) {if (a1 [I]. val = a1 [I-1]. val) {a [a1 [I]. index] = a [a1 [I-1]. index]; // equal processing} elsea [a1 [I]. index] = I; // unequal processing}

Complete code:

 

 

/* Function: calculates the number of reverse orders using the line segment tree */# include
       
        
# Include
        
         
Using namespace std; # define MAXSIZE 100000 # define M 300000 // compress struct arrayExtend {int val; int index ;}; bool cmp (arrayExtend ae1, arrayExtend ae2) {return ae1.val <ae2.val;} // struct node of the Line Segment tree {int left, right, sum;} t [M]; void build (int root, int start, int end) {// if they are equal, if (start = end) {t [root] is returned for the leaf node. sum = 0; t [root]. left = start; t [root]. right = end; return;} int mid = (start + end)> 1; build (ro Ot * 2 + 1, start, mid); // create the left subtree build (root * 2 + 2, mid + 1, end ); // create the right subtree t [root]. sum = 0; t [root]. left = start; t [root]. right = end;} long int getSum (int root, int qstart, int end) {if (qstart> end) return 0; // if the query interval is greater than the interval to be queried, the return value is if (t [root]. left> = qstart & t [root]. right <= end) return t [root]. sum; int mid = (t [root]. left + t [root]. right)> 1; if (qstart> mid) return getSum (root * 2 + 2, qstart, end); else re Turn getSum (root * 2 + 1, qstart, end) + getSum (root * 2 + 2, qstart, end );} // if the value is index, the interval sum + 1 containing index indicates the number of void update (int root, int index) {if (t [root]. left = t [root]. right) {t [root]. sum + = 1; return;} int mid = (t [root]. left + t [root]. right)> 1; if (mid <index) update (root * 2 + 2, index); else update (root * 2 + 1, index); t [root]. sum = t [root * 2 + 1]. sum + t [root * 2 + 2]. sum; // backtracking} int main () {int N; cin> N; int * A = new int [N + 1]; // use a struct to compress and compress the data in the range of 1 ~ The order between N remains unchanged. arrayExtend * a1 = new arrayExtend [N + 1]; for (int I = 1; I <= N; I ++) {cin> a1 [I]. val; a1 [I]. index = I;} sort (a1 + 1, a1 + N + 1, cmp); // sort a [a1 [1]. index] = 1; for (int I = 2; I <= N; I ++) {if (a1 [I]. val = a1 [I-1]. val) {a [a1 [I]. index] = a [a1 [I-1]. index]; // equal processing} elsea [a1 [I]. index] = I; // unequal processing} build (0, 1, N); long int ans = 0; for (int I = 1; I <= N; I ++) {ans + = getSum (0, a [I] + 1, N); // enter a [I] To find [a [I] + 1, n] Number of elements between them: update (0, a [I]); // update the line segment tree} cout <ans <endl; delete [] a1; delete []; return 0 ;}
        
       

Note that the number of nodes required to store a line segment tree is 2 ^ (ceil (logN) + 1)-1. In addition, the number of reverse orders is long. Otherwise, hihoCoder can only get 80 points for 39th weeks. Because the data is 10 ^ 5, the number of reverse orders can reach 5*10 ^ 9, while the int size is only 4*10 ^ 9 points.

 


 

 

Related Article

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.