POJ-2823 Sliding window (Getting Started with sliding windows)

Source: Internet
Author: User

An array of size N≤10 6 is given. There is a sliding window of size kWhich is moving from the very left of the array to the very right. can only see the kNumbers 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 kis 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 integers Nand kWhich is the lengths of the array and the sliding window. There is NIntegers 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

Test instructions: gives the number of n and the interval length m, and then the maximum and minimum values for each interval of length m
Idea: Because the scope of the topic is relatively large, nlogn algorithm can actually, because there is only a set of data, but we have him as a sliding window of the entry problem to parse, sliding window is a value to find an interval, a fixed interval length of an O (n) algorithm
Below first we are familiar with the next double-ended queue
Header file #include <deque>
Define deque<int> Q;
Head Insert Q.push_front ()
Head Delete Q.pop_front ()
Trailing Insert Q.push_back ()
Tail Delete q.pop_back ()
Header value Q.front ()
Tail value q.back ()

Sliding window is a maintenance of a queue, which contains the maximum value of the following table
The first one is the current interval maximum.
Give an example
5 6 4) 9 1
Our interval length is 2
Start 5 into the queue, and then because 6:5 is big, 5 is kicked out of the queue, 6 comes in, because the top of the queue is the maximum value in the interval.
Then 4 also to 6 back, because if 6 went out, 4 is the current maximum value
After 9:4 and 6 are large, you can replace the previous

Main idea: In order to save a monotonically descending sequence, bigger than he is directly updated, small back interval with a thought, and then judge whether the subscript is out of range, each number is up to the queue once, out of the queue once
And then this one, we use two double-ended queues to maintain a maximum value, a minimum value
#include <cstdio>#include<cstring>#include<cmath>#include<iostream>#include<deque>#include<vector>#include<algorithm>using namespacestd;intn,m;inta[1000001];intb[1000001];intc[1000001];d eque<int>qx,qn;intCNT;intMain () { while(SCANF ("%d%d", &n,&m)! =EOF) {         while(!qx.empty ()) Qx.pop_front ();  while(!qn.empty ()) Qn.pop_front ();  for(intI=0; i<n;i++) scanf ("%d",&A[i]);  for(intI=0; i<n;i++)        {             while(!qx.empty () &&a[i]>=A[qx.back ()])//judge whether the new addition is larger than the front, big words we will put the maximum value of the front, Qx.pop_back ();            Qx.push_back (i);  while(!qn.empty () &&a[i]<=A[qn.back ()]) qn.pop_back ();            Qn.push_back (i); if(i>=m-1)            {                 while(!qx.empty () &&qx.front () <=i-m) Qx.pop_front ();//We put out the number of intervals out of the queue b[cnt]=A[qx.front ()];  while(!qn.empty () &&qn.front () <=i-m) Qn.pop_front (); C[cnt++]=A[qn.front ()]; }        }         for(intI=0; i<cnt;i++)        {            if(i==0) printf ("%d", C[i]); Elseprintf"%d", C[i]); } printf ("\ n");  for(intI=0; i<cnt;i++)        {            if(i==0) printf ("%d", B[i]); Elseprintf"%d", B[i]); } printf ("\ n"); }}

POJ-2823 Sliding window (Getting Started with sliding windows)

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.