Monotonous queue-and acm pku poj 3250 and 2823 solution report

Source: Internet
Author: User

[Transfer]: http://blog.csdn.net/linulysses/article/details/5771084

  

Monotonous queue

Assume that the sequence {XI
} N
= X1
, X2
,..., Xn
There is a ordinal relation defined in <(here, it can also be <=,>,> =, etc. The specific ordinal relation is determined by the Application ). So, {XI
} N
Is {XI
} N
XJ1
, Xj2
,..., Xjk
Where, J1 <J2 <... <JK, for any JP <JQ, xjp
<Xjq
. This property is the monotonicity of the monotonic queue: the monotonicity between the subject and the elements.

Similar to a general queue, the element leaves the queue and enters the queue at the end of the queue. When a new element is added to a queue, the queue is still monotonically updated to ensure that some or even all elements in the queue are displayed at the end of the queue. For example, for the sequence {XI
} N = {1, 2, 6, 4, 0, 7}. When element 4 is in front of the queue, the queue is {1, 2, 6}; When element 4 is in the column, element 6 will depart from the end of the team to get a new monotonous queue {1, 2, 4 }. When element 0 is in the queue, all original elements in the queue are out, and the new queue is {0 }.

  

    /* remove element with index before f from head of mono queue */  
DeQueue(MonoQ,f)
while MonoQ.head.index is ahead of f do
//do something here
remove MonoQ.head
end
end

  

 

    EnQueue(MonoQ,elem)  
while MonoQ.tail >= elem do
//do something here
remove MonoQ.tail
end
append elem to MonoQ
end

  

 

From the pseudo-code above, we can see that each element can only be in the queue once at most, but only once (either in the queue head or at the team end). Therefore, the time complexity of maintaining a monotonous queue is O (n ). You can use arrays or linked lists. Do something here can be used for some aggregate computing.In an important observation, this calculation generally only depends on element subscript or elements about to be queued and elements about to be queued. If so, when we leave the element, we can safely remove it by performing a calculation related to the element.

 

Application-Max rectangle

 

Although not explicitly mentioned in the largest rectangular IV in my blog, the stack in this algorithm is a monotonous queue. The order relationship of elements is determined by the height of the straight column. When a new element enters the queue from the end of the team, remove a straight column that is no less than it, and calculate a possible solution (do something here in the above pseudo code) while removing it ).

 

Application-poj 3250

 

Question (not strictly translated): There are n heads of ox heads standing in the east to form a column. Each ox has a certain height and can see the head of the ox whose front height is lower than it until it is blocked by a ox whose front height is greater than or equal to its height. Calculate the sum of the number of heads that each ox can see. See: http://acm.pku.edu.cn/JudgeOnline/problem? Id = 3250

 

The simplest algorithm is to calculate the number of items that can be viewed by each cow, and then sum up to get the answer. The time complexity is O (n2 ). For this question, it must be TLE. Not available.

 

The number of items that each ox can see is determined by its subscript and the subscript of the first ox not lower than its subscript. At the same time, in order to calculate the number that a cow X can see, we have to wait until the first Ox Y is read at least below it. Then we can calculate the number that X can see. Assume that X is read before y (as long as the height of the cow is read in order). when X is read, the assumption that the number of cows that come before X depends on X has been calculated (we only need to read each cow, computing depends on the number of its cattle can see), then we can safely discard X, without affecting subsequent computing. These two properties are consistent with the properties required by the application monotonous queue. Therefore, you can try to apply monotonous queues.

 

Now let's strictly analyze this idea. Cow [1. N] stores the height of a cow. At the beginning, MonoQ is empty in the monotonous queue. Consider the current oX I. If the queue is empty, enter the queue. Otherwise, if the ox X at the end of the team is not higher than I, X will be outputted. At this time, I will be the first ox not lower than X in the east (otherwise, X should be outputted before that ), so the number of cows X can see is f (x) = I-X-1. Superimpose f (x) on ans (Initial Value: 0 ). Continue this process until the queue is empty or the team tail is higher than I. Then, enter I. After all the cows are taken into consideration, a cow with an infinitely high subscript of N + 1 will be added to the Team to ensure that all normal cows will arrive.

  

    ans := 0  
monoq := {}
cow[n+1] = infinity
for i from 1 to n+1 do
while monoq is not empty and monoq.tail.height <= cow[i] do
increase ans by (i-monoq.tail.index-1)
remove the tail element from monoq
end
append new element {height:cow[i],index=i} to tail of monoq
end
output ans

  

In this way, the complexity of maintaining a monotonous queue is O (n), and each ox scans only once. Therefore, the total time complexity is O (n ).

Application-poj 2823

The two Application Instances mentioned above do not involve team-lead departure. This question is just an opportunity for us to understand this application.

A sequence with n elements and a sliding window with a width of K. The sliding window slides from the left side of the sequence to the right side, and slides the distance of an element each time. Output the minimum and maximum values of each window in sequence.

Obviously, the complexity of the brute-force algorithm is O (NK ). The complexity of a monotonic queue is O (n ). Here we only discuss the minimum value. The method for the maximum value is similar. The general idea is to maintain a monotonically increasing sequence so that it can ensure that the element of the team header falls in the current window. If yes, MonoQ. Head is the solution of the current window. When a window slides, the new elements that are displayed in the queue are deleted from the queue header. The elements that have fallen out of the window are not accessed because the window has been moved from left to right ).

  

    x[1..n] is the sequence  
mins[1..n-k+1] is the mins of windows from 1 to n-k+1
monoq := {}
// initialize the monoq for the first window
for i from 1 to k-1 do
while monoq is not empty and monoq.tail.value < x[i] do
remove the tail element of monoq
end
append new element {index:i, value:x[i]} to the tail of monoq
end
for i from k to n do
while monoq is not empty and monoq.tail.value < x[i] do
remove the tail element of monoq
end
append new element {index:i, value:x[i]} to the tail of monoq
//remove stale element from head
while monoq.head.index <= i-k do
remove the head of monoq
end
ans[i-k+1] := monoq.head.value
end

  

It must be pointed out that this algorithm only applies when the window length is a fixed value. In addition, it is suitable for scheduled query. For exampleEachCalculate the minimum value for all windows. If ad hoc query is used and the Window Length changes, the monotonous queue is not suitable. In this case, the rmq data structure can be used.

Note: This article will continue to update my monotonous queue application (for some situations, we may analyze it in detail ).

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.