Talk about RMQ algorithm

Source: Internet
Author: User

Useless words: It seems a long time no more bo, bored to talk about algorithm bar ( mainly find water problems ).

Feel for beginners, the teacher taught this algorithm when not how to understand, recently (about 1, 2 months ago) The teacher also taught the LCA (recently, the public ancestor, the amount, can Baidu, I do not speak, may later write a blog about this) said that the LCA turn RMQ back to Earnest (Xue) XI (xi). Probably understand, the essence is DP.

Meet RMQ:

To learn RMQ, first you need to know what RMQ beg?

The RMQ is simply to find the maximum (minimum) of the interval.

What the? I don't understand!

Give me a chestnut:

1-2 9 10) 15 38-9

There are 7 numbers ( random loss ), RMQ is used to query the maximum value (minimum), but it is interval. For example, the maximum value of this interval for query [1,3] is 9.

That should make sense.

Brute Force Solution:

In fact, such a topic can be directly violent, right, a heavy loop enumeration interval is good.

However, if more than one query efficiency is obviously much lower, because the calculation may be repeated.

So someone will think of DP Bar.

RMQ Solution:

And the RMQ algorithm is a magical DP.

RMQ feeling is seldom used, but this DP thought can be a great inspiration!

Go to the--RMQ (for example, the maximum value)

F[I,J] represents the maximum value in this interval from the start of I to the i+2j (probably not very clear, is the J-th square of two)

What the? Why use 2j so strange thing? This is the magic of RMQ.

And then the initial value is obviously f[i,0]=a[i] (original array) right, because the interval maximum of 20 equals 11 is definitely the number. You never say [2,2] The maximum value of this interval in [3,3] this interval?

What about the spicy state transfer equation?

F[i,j]=max (F[i,j-1],f[i+2j-1,j-1])

I'm sure you don't understand this.

Draw a picture of yourself:

If the green is required to the maximum value of this interval, in fact, the main part of the blue period of the maximum value and red this interval of the maximum is OK, right,

The maximum value of the green interval is equal to Max (blue maximum, red maximum)

Because DP is recursive, the blue maximum and red maximum are known,

So the equation is f[i,j] (in this case [1,4] This interval, so i=1,j=2) =max (F[i,j-1] (refers to [] this interval, so i=1,j=1), F[i+2j-1,j-1] (referring to [3,4] this interval, so i=1+21=3 j=1 ))

It may not be easy to understand that J can be abstracted as [1,4] 4 numbers, j-1 is 2 numbers, 2 consecutive 2 numbers are known, then we can find the interval of 1 4 numbers.

Approximate code:

 for i=1  --N f[i,0]=a[i];//Initialize  for j=1 -- LOG2 (n)   for i=1--and   n    f[i,j]=max (f[i,j-1],f[i+ ( 1 << (J1)]);//RMQ State transfer equation

If you ask why J put out the loop, it's not enough to RMQ.

If J puts the inner loop, then the solve order becomes F[1..n,1]...f[1..n,log (n)] so that the maximum of 2 consecutive 2-digit intervals is not first calculated, and the DP is not possible.

So much to say, I hope it helps.

RMQ application is still not much, also this is LCA, suggest to learn is train of thought.

Reserved LCA site: (Time to write an LCA)

Talk about RMQ algorithm

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.