RMQ algorithm analysis and rmq Algorithm

Source: Internet
Author: User

RMQ algorithm analysis and rmq Algorithm

RMQ is an offline algorithm that quickly calculates the maximum value of a range. It pre-processes time complexity O (n * log (n) and queries O (1). Therefore, it is a fast algorithm, of course, this problem can also be solved using the line segment tree.


Problem: n ai numbers are provided, allowing you to quickly query the maximum value of a certain range.


Algorithm classification: DP + bit operations


Algorithm Analysis: this algorithm is based on the DP and bitwise operators. We use dp [I] [j] to indicate that the algorithm starts from the I bit, to the maximum or minimum value of I + 2 ^ j-1.

Then I can divide dp [I] [j] into two parts, the first part from I to I + 2 ^ (J-1)-1, the second part, from I + 2 ^ (J-1) to I + 2 ^ j-1 to the power of the power, we actually know that the latter of the binary number is twice the former one, then I --- I + 2 ^ j can be divided into two equal parts through 2 ^ (J-1), then the transfer equation is easy to write out.

Transfer equation:Mm [I] [j] = max (mm [I] [j-1], mm [I + (1 <(j-1)] [j-1]);

Code:

void rmq_isit(bool ok){    for(int i=1;i<=n;i++)        mm[i][0]=mi[i][0]=a[i];    for(int j=1;(1<<j)<=n;j++)    {        for(int i=1;i+j-1<=n;i++)        {            if(ok)                mm[i][j]=max(mm[i][j-1],mm[i+(1<<(j-1))][j-1]);            else                mi[i][j]=min(mi[i][j-1],mi[i+(1<<(j-1))][j-1]);        }    }}

Then, for any interval l -- r, we can also obtain the Interval Difference len = (r-l + 1 ).

Then, we use the k smaller than 2 ^ k <= len to divide the interval into two cross parts: l to l + 2 ^ (k)-1, the two parts from r-(1 <k) + 1 to r are easy to solve.

Query code:

int rmq(int l,int r){    int k=0;    while((1<<(k+1))<=r-l+1)        k++;    //printf("%d %d %d %d\n",l,l+(1<<k),r-(1<<k)+1,r-(1<<k)+1+(1<<k));    int ans1=max(mm[l][k],mm[r-(1<<k)+1][k]);    int ans2=min(mi[l][k],mi[r-(1<<k)+1][k]);    return ans1-ans2;}

By the way, write a exercise question: poj 3264 Balanced Lineup

Evaluate the Interval Difference, so it is very easy to use.


What is algorithm analysis?

Algorithm analysis is a quantitative analysis of the computing time and storage space required by an algorithm.
Algorithm (Algorithm) is a step for solving problems. It can be defined as a special method for solving a specific problem. In computer science, algorithms are described in computer algorithm languages. Algorithms represent accurate and effective methods for solving a class of problems with computers. Algorithm + Data Structure = program to solve a given problem that can be computed or solved. Different people can compile different programs to solve the same problem. There are two problems: first, there are algorithm problems closely related to the computing method, and second, the technical problems of program design. There is a close relationship between algorithms and programs. The analysis algorithm can predict the environment in which this algorithm is suitable for effective operation and compare the effectiveness of different algorithms that solve the same problem.

What is the use of algorithm analysis?

In fact, you have explained that the research on more efficient algorithms is to save time. Have you studied numerical analysis? You know that if there are no efficient algorithms, you can find the 20x20 matrix based on the matrix definition. The current computer is never enough.
Where did the API come from? Can the one you wrote be adopted by sun? If you do not study sorting algorithms, isn't the code written in the same way as you?
Cloud, have you heard of it? The number processed now exceeds your imagination. One hundred degrees for example, every day processing a massive amount of data, you need to find something, it will take a few seconds to come out, then do not study algorithms, can you do it .?
Algorithms are especially important as data grows and increases.
Algorithms are actually used to train your own thinking. There are different solutions to a problem. When you encounter a new thing, you may write algorithms, but you may not write code. Zhidao.baidu.com/question/422543292.html? Oldq = 1. For example, I think of algorithms.
In addition, the technical content of coding is not high, and it is a clever process.

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.