Poj 2452 rmq + binary

Source: Internet
Author: User

A given array is used to find the longest sub-array si... SJ, so that the Si in the sub-array is the minimum value and the SJ is the maximum value.

There is nothing to say about the algorithm. What needs to be summarized is the way of thinking when I do the question.

You can use rmq to find the maximum and minimum values in an array. The solution I started with was to enumerate the Interval Length, And the complexity was O (N ^ 2) and TLE. You can only change your mind to enumerate the start point and find the maximum range of the start point. I assume that when I is the start point, r = rmqmax (I, n) is the end point, and then I is determined whether rmqmin (I, R) is equal to I, in this enumeration, the complexity is only O (n ). However, this is wrong because when I is the start point, the end point of the maximum interval is not necessarily R, which may be on the left of R. The maximum interval may be ignored.

We often encounter this situation when we are doing questions. We think that the correct method is actually wrong. Think about it. If we are stubborn to believe in ourselves in the game, it may be stuck in a place like this. How does this happen? When thinking about the solution, every step is a process of reasoning. We think that this step is correct. In fact, we assume that our reasoning is correct, obtain the Final Solution step by step. Unfortunately, for various reasons, we often cannot come up with a correct assumption every time, but we believe that we are correct. So how can we make the correct assumptions? When reflecting on the process of thinking, we often make various guesses before we can get a definite conclusion, and then verify them one by one to find the correct results. However, during the verification process, you mayIt seems that the correct result is the correct result.Without rigorous reasoning, this should be a bad habit of thinking formed for a long time. Therefore, to avoid making incorrect assumptions, we must overcome our less rigorous assumptions and strictly prove our reasoning. This is a painful process, but most of the problems we encounter are not too hard to prove. Intuitive explanations are enough.

To sum up, we must ensure that the conjecture is correct when making a guess to solve the problem, rather than seemingly correct.

Back to this question, enumerate the start point, and then find the maximum end point. Now the problem is to find the end point. Starting from the beginning, each time we verify L [rmqmin (I + 1, J)]> L [I], and L [J]> L [rmqmax (I, J-1)], find the maximum length. The result is correct, but the complexity returns to O (N ^ 2). Then, verify L [rmqmin (I + 1, J)]> L [I], and L [J]> L [rmqmax (I, J-1)], the process of separation, first find meet L [rmqmin (I + 1, J)]> the maximum interval of L [I]. The position of the maximum value on I and j is the end point. This reasoning is correct. Find the maximum interval that meets the requirements of L [rmqmin (I + 1, J)]> L [I]. You can find that, starting from the start point and traversing to the right, either l [rmqmin (I + 1, J)]> L [I] is met, or after a certain point, all vertices do not meet L [rmqmin (I + 1, j)]> L [I], which can be regarded as monotonic in a broad sense, or equivalent to a function. The initial value of the function is 0, which is changed to 1 after a certain point, in this way, you can use the binary message to find the critical point.

Now, we can determine the solution to this problem. First, we use rmq to pre-process the problem so that we can find the maximum value in a certain range within the O (1) Time, And then enumerate the start point, find the end point using the bipartite method to obtain the longest interval.

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.