POJ 2823 Sliding Window (monotone queue)

Source: Internet
Author: User

Sliding Window
Time Limit: 12000MS Memory Limit: 65536K
Total Submissions: 46705 Accepted: 13485
Case Time Limit: 5000MS

Description

An array of sizeN≤106 is given to you. There is a sliding window of sizekWhich is moving from the very left of the array to the very right. can only see thekNumbers 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], andkis 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
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 integersNandkWhich is the lengths of the array and the sliding window. There isNIntegers 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

Source

POJ monthly--2006.04.28, Ikki about the monotone queue:

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.

Http://www.cnblogs.com/Jason-Damon/archive/2012/04/19/2457889.html

That's a good point. I think the focus of the place is color-added.

If you want to find the maximum value, then the element is smaller and older than the newly added elements are impossible to become an answer, you can safely delete!

Understanding this sentence to understand the monotonous queue ...

The above blog will be very good, but the code is slightly ugly.

I wrote a thought that I could see through.

/************************************************************************* > File name:code/2015summer/monotone queue/ B.cpp > Author:111qqz > Email: [email protected] > Created time:2015 July 29 Wednesday 20:18 16 seconds *********** *************************************************************/#include<iostream>#include<iomanip>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>#include<string>#include<map>#include<Set>#include<queue>#include<vector>#include<stack>#defineY0 ABC111QQZ#defineY1 HUST111QQZ#defineYn hez111qqz#defineJ1 CUTE111QQZ#defineTM CRAZY111QQZ#defineLR DYING111QQZusing namespacestd;#defineREP (i, n) for (int i=0;i<int (n); ++i)typedefLong Longll;typedef unsignedLong LongULL;Const intINF =0x7fffffff;Const intn=1e6+7;intA[n],mxans[n],q[n],indx[n],mians[n];intn,k;intHead,tail;intMain () {CIN>>n>>K;  for(inti =0; I < n; i++) {scanf ("%d",&A[i]); } head=1; Tail =0;  for(inti =0; I < n; i++)    {    if(indx[head]+k-1<i) {head++; }     while(Head<=tail&&q[tail]<=a[i]) tail--; Tail++; Q[tail]=A[i]; Indx[tail]=i; if(i>=k-1) {mxans[i-k+1]=Q[head]; }} head=1; Tail =0; memset (q,0,sizeof(q));  for(inti =0; I < n; i++)    {    if(indx[head]+k-1<i) {head++; }     while(Head<=tail&&q[tail]>=a[i]) tail--; Tail++; Q[tail]=A[i]; Indx[tail]=i; if(i>=k-1) {mians[i-k+1]=Q[head]; }    }     for(inti =0; I < n-k+1; i++ )    {    if(i==0) cout<<Mians[i]; Elsecout<<" "<<Mians[i]; } cout<<Endl;  for(inti =0; I < n-k+1; i++ )    {    if(i==0) cout<<Mxans[i]; Elsecout<<" "<<Mxans[i]; }      return 0;}

Poj 2823 Sliding Window (monotonic queue)

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.