Your task is to determine the maximum and minimum values in the sliding window at each position.
Look at this question: An array of size n ≤106 is given to you. There is a sliding window of size K which was moving from the very left of the array to the very right. You can only see the K numbers in the window. Each of the sliding window moves rightwards by one position. Your task is to determine the maximum and minimum values in the sliding window at each position.
That is, there is a sequence a that asks you to ask for the minimum value in Series B and C,b[i] (a[i]...a[i+w-1], c[i] is the maximum value. If A is 1,3,-1,-3,5,3,6,7, then B is -1,-3,-3,-3,3,3,c for 3,3,5,5,6,7.
This problem is equivalent to a stream of data (sequence a) is constantly coming, and the data is constantly expiring, the equivalent of we can only save limited data (sliding window of the data, this problem is the width of the Windows W), for the arrival of the query (the query in this topic is every moment), We want to return the maximum value \ Minimum value in the current sliding window. Note that the element is constantly expiring.
To solve this problem, you can use a data structure called a monotone queue that maintains a queue like this:
A) from the team head to the end of the team, the elements are decreasing (strictly decreasing, not non-incrementing) under the indicator we are concerned with, for example, if the query is the minimum value within the window, then the elements in the queue should be incremented from left to right, and if the maximum value within the window is queried each time, then the This is to ensure that each query only needs to take the team head element.
b) from the head to the end of the team, the element corresponding to the moment (in this case, the element in the sequence a subscript) is incremented, but does not require continuous, this is to ensure that the leftmost element is always the first to expire, and every time a new element comes, it must be inserted at the end of the team.
The queue that satisfies the above two points is the monotone queue, first of all, only the sequence of the first element must be a monotone queue.
So how do you maintain this monotonous queue? It is nothing more than handling inserts and querying two operations.
For insertion, because of the nature of B, the new element that comes in is inserted into the queue and the last can be maintained B) continues to be established. But in order to maintain the establishment of a, that is, the element is decreasing under the index of our concern, it is possible to remove elements from the tail when inserting new elements from the end of the team, specifically, to find the first element that is greater than (under the target of interest) The new element, delete all subsequent elements, and insert the new element behind it. Because all the deleted elements are smaller than the new elements and older than the new ones, it is impossible to be an answer in any future queries, so you can safely delete them.
For queries, because of the nature of B, all elements that expire at that time must be focused on the team header, so use the time of the query to delete all expired elements of the team header, without the expiration element, the team header element is the answer to the query (Nature a), return it.
The amortization complexity is O (n) because each element is in the queue once.
POJ2823 is the problem described above.
That's a good point. I think the focus of the place is color-added.