POJ 2823 Sliding Window ST RMQ

Source: Internet
Author: User

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

Source

POJ monthly--2006.04.28, Ikki Test Instructions:given a sequence of long n, ask the minimum and maximum of all the cases in the series with a length of K. Ideas:Seniors Teach the RMQ solution, St version of the essence is DP, than do not understand DP before, now feel good understanding more. In addition, you can use segment tree solutions. Note the Log table first.
1#include <stdio.h>2#include <algorithm>3 //#define LOG[I] = (I & (i-1))? LOG[I-1]: log[i-1] + 14 #defineMAXX 12345675#include <vector>6 using namespacestd;7 8 intA[maxx];9 intdp1[maxx][ A];Ten intLog[maxx]; One  A voidInitintN) - { -log[1] =0;  the      for(intI=2; i<=n; i++) -log[i]= (i& (i-1))? log[i-1]:log[i-1]+1; - } -  + intST (intLintRinti) - { +     intk=log[r-l+1]; A     if(i==1) at         returnMax (dp1[l][k],dp1[r-(1&LT;&LT;K) +1][k]); -     if(i==0) -         returnMin (dp1[l][k],dp1[r-(1&LT;&LT;K) +1][k]); - } - intMain () - { in     intN, K; -      to      while(~SCANF ("%d%d", &n, &k)) +     { -         intI, J; the init (n); *          for(i=1; i<=n; i++) $         {Panax Notoginsengscanf"%d", &a[i]); -dp1[i][0]=A[i]; the         } +          for(j=1; j<= -; J + +) A         {  the              for(i=1; i<=n; i++) +             { -                 if(I+ (1&LT;&LT;J)-1>N) $                      Break; $Dp1[i][j]=min (dp1[i][j-1], dp1[i+ (1<< (J-1))][j-1]); -             } -         } the          for(i=1; i<=n-k+1; i++) -         {Wuyi             if(i!=1) theprintf" "); -printf"%d", ST (i,i+k-1,0)); Wu         } -         ////// About  $          for(i=1; i<=n; i++) -         { -dp1[i][0]=A[i]; -              for(j=1; j<= -; J + +) Adp1[i][j]=0; +         } the          for(j=1; j<= -; J + +) -         {  $              for(i=1; i<=n; i++) the             { the                 if(I+ (1&LT;&LT;J)-1>N) the                      Break; theDp1[i][j]=max (dp1[i][j-1], dp1[i+ (1<< (J-1))][j-1]); -             } in         }  theprintf"\ n"); the          for(i=1; i<=n-k+1; i++) About         { the             if(i!=1) theprintf" "); theprintf"%d", ST (i,i+k-1,1)); +         } -printf"\ n"); the     }Bayi} 

POJ 2823 Sliding Window ST RMQ

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.