Multiplication algorithm RMQ

Now a question for you: to give you an array of N numbers, and now to ask you again to give you the interval [l, r], ask your maximum within this interval is how much?

 

In fact, we learned before this issue can be resolved tree line, we use a tree line to maintain maximum range on it. but! If the number of queries we are more, then the tree line this solution is clearly not an optimal solution. So here we introduce a new solution ------- ST table

 

RMQ (the Range Minimum / the Maximum Query) , that is, the interval value most queries, this is an online algorithm , the so-called online algorithms, means every time the user enters a query, it will immediately process a query  RMQ preprocessing algorithm is generally used for a long time , time complexity is O (nlogn), then process each query in O (1) time.

 

We assume that the array arr is: 1,2,6,8,4,3,7

We set up a two-dimensional array dp [i] [j] represents the minimum value of the number of consecutive bits from the i starts . E.g. dp [2] [1] indicates the start to the minimum of the two consecutive number (i.e., the third digit to the second digit from the minimum value) from the second number of bits, i.e., the minimum value of 2,6, Therefore dp [2] [1] = 2;

 

In fact, we seek dp [i] [j] when you can divide it into two parts, the first part is from that second part to why it can be so divided? In fact, we all know that a binary number is a number in front of the double, you can put into this section by being divided into two equal parts, then transfer equation it is easy to write out. ( DP [i] [0] indicates the i-th digit to itself )

 

dp[i][j] = min(dp [i][j - 1], dp [i + (1 << j - 1)][j - 1])

Thereby giving the following code:

1 void rmq_init(){
2     for (int i=1;i<=n;i++)
3         dp[i][0] = a[i];
4     for (int j=1;(1<<j)<=n;j++){
5         for (int i=1;i+(1<<j)-1<=n;i++){
6             dp[i][j] = min(dp[i][j-1],dp[i+(1<<j-1)][j-1]) 
7         }
8     }
9 }

 

It should be noted that a variable loop sequence, we see the outer loop variable j, the inner loop variable i, which is why? You can be interchanged locations?

 

The answer of course is no, we have to understand the significance of this state transition equation, the meaning of this equation of state is: to update minimum of every two elements, then get each four elements each by a minimum of two elements minimum, and so updates the minimum value of all lengths.

Boundary conditions are a little explanation of how this program to determine:

(1 << j) <= n: j because in fact reflect an essence of this program -> doubled, obviously I can not be longer than the length of the array ah

(I + (1 << j) -1) <= n: as dp [i] [j] is the maintenance interval [i, i + 2 ^ j-1], so that right can not be out of range

 

Next we explain the query part of RMQ

Suppose we need, the minimum interval [l, r] in, so that k =

 

The interval [l, r] minimum RMQ [l, r] = min (dp [l] [k], dp [r - (1 << k) + 1] [k]);

 

But why so that it can ensure a minimum interval of it?

 

dp [l] [k] is maintained in the interval [l, l + 2 ^ k - 1], dp [r - (1 << k) + 1] [k] is maintained in the interval [r - 2 ^ k + 1, r].

So long as we guarantee   ≤ 

We can guarantee RMQ [l, r] = min (dp [l] [k], dp [r - (1 << k) + 1] [k]);

Here you can go hand push

 

1 int rmq(int l,int r){
2     int k = log2(r-l+1);
3     return min(dp[l][k],dp[r-(1<<k)+1][k]);
4 }

 

 

This is the multiplication algorithm RMQ, I is the minimum range, for example, the maximum interval actually the same is not to say.

 

 

 

When I saw this blog is a doubling understand RMQ: https://blog.csdn.net/qq_41311604/article/details/79900893

 

 

Guess you like

Origin www.cnblogs.com/-Ackerman/p/11311543.html