poj--2823--sliding Window----Monotone queue problem

Source: Internet
Author: User

Sliding Window

Time limit:12000MS Memory Limit:65536KB 64bit IO Format:%i64d &%i64u

Description

An array of size n ≤10 6 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
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 question means:
Given a sequence of numbers, the length of the sequence that can be seen every time m is the length of the window, moving the window from left to right, and outputting the maximum and minimum values of the sequence length that can be seen each time, until the end of the sequence.
Ideas:
In order to minimize the value of the case,
1. Constructs the increment of the monotone queue, that is, the tail element needs to be less than the current element, otherwise the tail element is out of the team.
2. First, the first m-1 elements into the team according to the monotone queue principle, and records each element in the queue in the array subscript;
Second, the remaining elements in order to enter the team, with the element I as an example, the element I and the tail of the team, if less than the team tail, the team tail out of the team, or the current element into the team, the queue is monotonically increasing order,
Again, the team head element subscript and the current element of the subscript comparison, if the current element subscript-Team head element subscript <=m-1, the team head element is the minimum value, otherwise the team head out of the team.
3. Take the remaining elements sequentially to step 2, and then get all the minimum values you ask for, and the maximum value.
(please submit in C + +, otherwise time Limit exceeded):
1#include <iostream>2#include <stdio.h>3#include <cstring>4#include <algorithm>5#include <cmath>6#include <vector>7#include <map>8#include <string.h>9#include <stack>Ten#include <Set> One#include <queue> A using namespacestd; - inta[1000005]; - //Subscript for each element in the queue the intp[1000005]; - //maximum values for each interval - intmax1[1000005]; - //minimum values for each interval + intmin1[1000005]; - //array emulation increment queue + intqmax[1000005]; A //array emulation Descending queue at intqmin[1000005]; - //team head, Team tail - intHead,tail; - intMain () - { -     intn,m,i,t; in      while(~SCANF ("%d%d",&n,&m)) -     { to          for(i =1; i<=n; i++) scanf ("%d", A +i); +         //Team head End initialization -Head =1; theTail =0; *t =1; $         //first m-1 elements into the queuePanax Notoginseng          for(i =1; i<=m-1; i++) -         { the              while(Head<=tail&&qmin[tail]>=a[i]) tail--; +Qmin[++tail] =A[i]; A             //Subscript for each element in the queue theP[tail] =i; +  -         } $         //ask for all minimum values $          for(; i<=n; i++) -         { -             //into the team the              while(Head<=tail&&qmin[tail]>=a[i]) tail--; -Qmin[++tail] =A[i];WuyiP[tail] =i; the             //determine if the opponent is within the current scope -              while(i-p[head]>m-1) Wuhead++; -min1[t++] =Qmin[head]; About         } $Head =1; -Tail =0; -t =1; -         //ask for all the maximum values A          for(i =1; i<=m-1; i++) +         { the              while(Head<=tail&&qmax[tail]<=a[i]) tail--; -Qmax[++tail] =A[i]; $P[tail] =i; the         } the          for(; i<=n; i++) the         { the             //into the team -              while(Head<=tail&&qmax[tail]<=a[i]) tail--; inQmax[++tail] =A[i]; theP[tail] =i; the             //determine if the opponent is within the current scope About              while(i-p[head]>m-1) thehead++; themax1[t++] =Qmax[head]; the         } +          for(i =1; i<t; i++) -         { the             if(i = =1)Bayiprintf"%d", Min1[i]); the             Else theprintf"%d", Min1[i]); -         } -printf"\ n"); the          for(i =1; i<t; i++) the         { the             if(i = =1) theprintf"%d", Max1[i]); -             Else theprintf"%d", Max1[i]); the         } the 94     } the     return 0; the}


This article is a personal essay, if there are inappropriate, hope that the big boys more advice.
If you can provide small help to Bo friends, it is a great honor.

poj--2823--sliding Window----Monotone queue problem

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.