POJ 2823 Sliding Window

Source: Internet
Author: User

Sliding Window

Description

An array of size N≤106 is given to you. 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
A sequence of length n, with the length of the window to see K on the top of the move, to find the maximum number of Windows.
Analysis:
The monotonous queue asks, the entry question has received. 、

1# include <iostream>2 using namespacestd;3 4 Const intMAX =1000010;5 intA[max];6 intQ[max];7 intP[max];8 intMin[max];9 intMax[max];Ten  One intN, K; A  - voidget_min () - { the     intHead =1, tail =0; -      for(inti =0; I < K-1; i++) -     { -          while(Head <= tail && Q[tail] >=A[i]) +tail--; -tail++; +Q[tail] =A[i]; AP[tail] =i; at     } -      for(inti = k-1; I < n; i++) -     { -          while(Head <= tail && Q[tail] >=A[i]) -tail--; -tail++; inQ[tail] =A[i]; -P[tail] =i; to          while(P[head] < I-k +1) +         { -head++; the         } *Min[i-k +1] =Q[head]; $     }Panax Notoginseng } - voidGet_max () the { +     intHead =1, tail =0; A      for(inti =0; I < K-1; i++) the     { +          while(Head <= tail && Q[tail] <=A[i]) -tail--; $tail++; $Q[tail] =A[i]; -P[tail] =i; -     } the      for(inti = k-1; I < n; i++) -     {Wuyi          while(Head <= tail && Q[tail] <=A[i]) thetail--; -tail++; WuQ[tail] =A[i]; -P[tail] =i; About          while(P[head] < I-k +1) $         { -head++; -         } -Max[i-k +1] =Q[head]; A     } + } the  -  $  the intMain () the { thescanf"%d%d", &n, &k); the      for(inti =0; I < n; i++) -scanf"%d", &a[i]); in get_min (); the Get_max (); the      for(inti =0; I < N-k +1; i++) About     { the         if(i = =0) theprintf"%d", Min[i]); the         Else +printf"%d", Min[i]); -     } theprintf"\ n");Bayi      for(inti =0; I < N-k +1; i++) the     { the         if(i = =0) -printf"%d", Max[i]); -         Else theprintf"%d", Max[i]); the     } theprintf"\ n"); the     return 0; -}
View Code


POJ 2823 Sliding Window

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.