Simple analysis of monotone queue
by Zhonghuanlin
September 16 2014 Update: September 16 2014
Article folder
- 1.Simple monotone queue applications:
- 1.1. 1. Fruit Merger Issues
- 1.2. Window
- 1.2.1. poj2823
- 1.3. Advertising Printing
- 2. the application of monotone queue in dynamic programming
- 3. References:
You should know what a queue is, then add "monotone" to the queue. The meaning is also obvious. Is that this queue is monotonically increasing or monotonically decreasing from the previous.
For example: {a1,a2,a3,a4......an} satisfies the a1<=a2<=a3......<=an,a sequence is a monotonically increasing sequence.
Similarly, descending queues are also present.
单调队列的出现能够简化问题,队首元素便是最大(小)值,这样。选取最大(小)值的复杂度便为o(1),因为队列的性质,每一个元素入队一次,出队一次,维护队列的复杂度均摊下来便是o(1)。
How to maintain the monotone queue, take the monotone increment sequence as an example:
Assuming that the length of the queue is certain, first infer whether the first element of the team is within the specified range, assuming that the super-range increases team head.
Each time the element is incremented and the tail is relatively low, the tail pointer is reduced if the current element is less than the tail and the queue is non-empty. The tail element is then out of the team until the queue is tuned to fit.
Like what:
queue is a monotonically increasing queue: 1, 5, 7, 9. Now you want to insert a 6. Because of 9, 6 , so 9 out of queue ---; 1 , 5 , 7 .
由于要 7 > 6 ,所以 7 出队列---> 1, 5 . 6放在队列尾部,终于队列变为:---> 1,5,6.
The nature of the monotonous queue is finished. So how do we use it?
Simple monotone queue applications: 1. Fruit merger problem
"Description of the problem"
In an orchard. Toto has beaten all the fruits and divided them into different heaps according to the different kinds of fruit. A lot decided to synthesize all the fruits.
With each merger, many can merge the two piles of fruit together. The energy consumed equals the sum of the weight of two of the fruits.
Can see. After all the fruits have been n-1, there is only a heap left. The total amount of energy consumed in the merging of fruits equals the physical strength of each merger.
As it takes a lot of effort to bring these fruits home, it is necessary to save as much energy as possible when merging the fruits. Assuming that each fruit weighs 1, and that the number of fruit species and the number of each fruit is known, your task is to design a combination of sequential schemes that will consume the least amount of energy and output this minimum physical cost.
For example, there are 3 kinds of fruit, the number is 1, 2. 9. It is possible to merge 1 and 2 stacks, and the number of new heaps is 3, which consumes 3 energy. Then. Merges the new heap with the original third heap. And get a new heap. The number is 12. Consumes 12 of the energy. So the total cost of energy =3+12=15. The ability to prove that 15 is the minimum physical cost value.
"Input File"
The input file fruit.in consists of two lines, and the first line is an integer n (1<=n<=10000) that represents the number of species of fruit. The second line consists of n integers. Separated by a space, the I-integer AI (1<=ai<=20000) is the number of the fruits of the first I.
"Output File"
The output file Fruit.out includes a single line, which consists of only an integer, which is the minimum physical cost. The input data guarantees that this value is less than 231.
"Example Input"
"Example Output"
"Data Size"
对于30%的数据,保证有n<=1000: 对于50%的数据。保证有n<=5000; 对于所有的数据,保证有n<=10000。
The subject is very classic and very much distributed. The idea of being able to use a fast row or a heap is to merge with the current minimum of two stacks.
The complexity is O (NLOGN), assuming an orderly queue maintenance with a time complexity of O (n).
Each time you select two heaps to merge, it is not the first given heap, or the new heap that is obtained after merging the initial heap several times. Therefore, two monotone increment queues need to be maintained. A queue is stored with the value of the originally given heap (1), and a new value (2) is obtained after the merge is saved.
There are three states for each selection:
Pick Team One's first two
Select Team 2 for the first two
Choose one of the first teams
You only need to make corresponding changes to each of the queue's pointers.
Pay special attention to initialization.
This problem is very good use of the problem of the monotony of decision-making, the initial alignment of the line to ensure its monotonicity. And for the newly generated heap. Once new elements are added. The new element must be greater than the original element. (very obviously, because of the monotony of queue 1).
In other words, the monotony of the queue is natural.
There is no need for maintenance.
To be good at observation and analysis, talent found.
Windowpoj2823
Description
An array of size n≤106 are 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. Following is an example:
The array is [1 3-1-3 5 3 6 7], and K is 3.
window position |
minimum value |
maximum value |
[1 3-1 "-3 5 3 6 7 |
-1 |
3 |
1 [3-1-3] 5 3 6 7 |
-3 |
3 |
1 3 [ -1-3 5] 3 6 7 |
-3 |
5 |
" TR style= "" >
1 3-1 [-3 5 3] 6 7 |
-3 |
5 |
1 3-1-3 [5 3 6] 7 |
3 |
6 |
1 3-1-3 5 [3 6 7] |
3 |
7 |
Your task is to determine the maximum and minimum values in the sliding window at each position.
Input
The input consists of the lines. The first line contains the integers n and K which is the lengths of the array and the sliding window. There is n integers in the second line.
Output
There is lines in the output. The first line gives the minimum values in the windows at each position, from left to right, respectively. The second line gives the maximum values.
Sample Input
8 31 3 -1 -3 5 3 6 7
Sample Output
-1 -3 -3 -3 3 33 3 5 5 6 7
The main idea: give a group of numbers. A fixed-size window slides on this array. Requires the maximum and minimum values to be slid in the window each time.
This is the typical monotone queue, where the function of the monotone queue is. The first line of the monotone queue is the maximum value within the interval, but the entire queue does not have to be monotonous.
The maximum and minimum values are handled with two queues, respectively. The maximum value is explained here;
When a value of num is added to the queue. Swipe from the end of the queue until a D value less than Num is encountered, inserting num into the next bit of D.
All subsequent elements are invalidated (regardless of the subsequent element). When looking for the maximum value. Start looking for the team first. If the element is not in the range at this time. Find the next. Until the first element that satisfies the condition is found. This element is the most value.
The minimum and maximum values are much the same, and you just need to change the condition of the increment num to be able.
Advertising printing
"Description of the problem"
Recent. Afy decided to give Toj print ads, billboards are painted on the city's buildings. There are n buildings in the city that are close to each other.
Afy decided to place a billboard on top of a rectangle as large as possible. If every building has a height, from left to right, the height of each building is H1,H2 ... Hn. And 0
"Input File"
The first line is a number n (n<= 400,000)
The second line is the number of N. The height of each building is H1,H2 ... HN, and 0
"Output File"
The output file ad.out a common line that represents the maximum area of the billboard.
"Input Example"
65 8 4 4 8 4
"Output Example"
24
Analysis
Finally the billboard must be equal to the height of a building x the maximum length it can reach
Today, the height of the building is known, and it is now only necessary to know how long each height can reach. Because N is 400000. We can only use O (n) or O (NLOGN) algorithms. Ability to use RMQ. I'll talk about it in the paper that's behind.
Now we are talking about the method of the monotone queue with time complexity O (n).
Continue with the idea that for each building, you just need to find the maximum width it can extend to.
That is, the right and left side of the building is lower or equal to its number of buildings.
How to use the monotonous queue?
We entered the team from 1~n. Maintains a monotonically decreasing sequence. Each time the element is added to maintain its monotonicity, of course this will certainly make some elements out of the team. The elements of the team must be smaller than the currently added elements, meaning that the current element is the farthest building on the right side of the element that is coming out of the team!
Note that for h[n+1]=0 and the element to be enqueued once (which causes all elements in the current queue to go out), each element is guaranteed to have its "right limit" value.
Ask for the same "left limit". Just need to cycle from n~0, note 0
This problem is used for the deformation of the monotone queue. Because the result of the problem is monotonic. Very good use of the characteristics of the team elements.
The application of monotone queue in dynamic programming
When you do dynamic planning, you often see a form such as this transfer equation:
F[X] = Max or min{g (k) | b[x] <= k < x} + W[x]
(Middle B[x] with x monotony, that is, B1<=b2<=b[3]<=...<=b[n])
(G[k] denotes a function related to K or f[k], w[x] represents a function related to x)
How is this equation solved? We note the nature of the hypothesis that there are two numbers J, K, which makes J <= K. and G (k) <= G (j). The decision J is useless. Due to the monotonic nature of the b[x], assuming that J can act as a legitimate decision, K must be able to act as a legitimate decision, and because K is better than J, (note: In this classic model, "excellent" is absolute and is independent of the state currently being computed), so to speak. Assuming that the decisions in the decision table are sorted by K, then G (k) must not fall.
Such It leads us to use a monotone queue to maintain the decision table.
For each State f (x), the calculation process is divided into the following steps:
The first element of the team is out of the team until the team first element is in the given range.
At this point, the first element of the team is the optimal decision of State F (x).
Calculates g (x). and insert it into the end of the monotonic queue, maintaining the monotony of the queue at the same time (continuously out of line until the queue is monotonous).
Repeat the above steps until all of the function values are calculated. It is not difficult to see that this algorithm averaging time complexity is O (1). The time complexity of solving f (x) is thus reduced from O (n^2) to O (n).
Monotone queue refers to a queue of all the number of monotonic (monotonically increasing or monotone), in the application of some topics in the informatics competition, will reduce the complexity of time
Each element of a monotonic queue typically stores two values:
1. Position in the original sequence (subscript)
2. The status value (value) of the element in dynamic planning
monotonic queues ensure that both values are monotonic at the same time.
Beacon Transmission
Descriptive narrative Description
Beacon is also called Beacon. It is an important defensive facility, usually built on an advantageous location or a traffic thoroughfare. Once the enemy has been in place. Burning firewood during the day, expressing information through smoke: Burning dry wood at night, passing military intelligence in flames.
There are n beacons between the two cities, and each beacon signal has a price. For the accurate transmission of intelligence. At least one of the M beacons should be signaled. Now enter the price of N, m and each beacon signal, please calculate the total cost of the minimum amount of money, the ability to make the enemy attack. Intelligence can be accurately transmitted between the two cities!.
Input format
第一行有两个数n,m分别表示n个烽火台,在m个烽火台中至少要有一个发出信号。 第二行为n个数。表示每个烽火台的代价。
Output format
一个数。即最小代价。
Example
Input:
5 3 1 2 5 6 2
Output:
4
Time limit limitation
各个測试点1s
Gaze Hint
1<=n,m<=1,000,000
Analysis
It needs to be solved by dynamic planning method. We are able to write this equation F[i]:=min{f[j]}+a[i] (i-m<=j<i-1) (because it is necessary to ensure that 3 of the preceding I must have a lighted beacon). Simply looping this way can cause timeouts.
We thought of optimizing with a monotone queue, because with the loop of I, there was only one I out of the decision interval, only one I out-of-band, because each time the minimum value in the decision interval was selected, a monotonically increasing sequence was maintained. Each time you take out the first element of the team.
Why is it possible to delete the elements of the end of the team mercilessly? Because the sequence of the backward team satisfies the position in the original sequence at the same time and its value in the dynamic planning is greater.
Selecting this element is better than selecting a decision before whichever one is selected. So the decisions that were deleted before are useless.
The essence of this problem is to use a monotone queue to maintain the value of the decision itself and its position in the original sequence at the same time monotonous.
Pay special attention to the value in the monotone queue as the position of the decision in the original decision sequence.
References:
Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.
In a monotonous queue