POJ#2823. Sliding Window

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ#2823. Sliding Window相关的知识,希望对你有一定的参考价值。

题目描述

An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time 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 positionMinimum valueMaximum 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. 

输入格式

The input consists of two lines. The first line contains two integers n and k which are the lengths of the array and the sliding window. There are n integers in the second line. 

输出格式

There are two lines in the output. The first line gives the minimum values in the window at each position, from left to right, respectively. The second line gives the maximum values. 

样例输入输出

输入

8 3
1 3 -1 -3 5 3 6 7

输出

-1 -3 -3 -3 3 3
3 3 5 5 6 7

简单的单调队列 。如果以G++提交要加输入和输出优化,否则要t
 1 #include <iostream>
 2 #include <cstdio>
 3 const int N = 1000000 + 11 ;
 4 using namespace std ;
 5 int n , k , sum[N]  , q1[N] , q2[N] ,  minn[N] , maxx[N] ; 
 6 
 7 int read( )
 8 {
 9     char ch = getchar( ) ; int k = 1 , ret = 0 ;
10     while( ch < 0 || ch > 9 ) { if( ch == - ) k = -1 ; ch = getchar( ) ; }
11     while( ch <= 9 && ch >= 0 ) ret = ret * 10 + ch - 0 , ch = getchar( ) ;
12     return ret * k ;  
13 }
14 
15 
16 void Init( )
17 {
18     for( int i = 1 ; i <= n  ; ++i ) sum[i] = read( ) ;
19     
20 }
21 
22 void Solve( )
23 {
24     int l1 = 1 , r1 = 0 , l2 = 1 , r2 = 0 ; q1[1] = q2[1] = 0 ;
25     for( int i = 1 ; i <= n ; ++i )
26     {
27         while( l1 <= r1 && sum[q1[r1]] < sum[i] ) --r1 ;
28         q1[++r1] = i ; 
29         while( l1 <= r1 && q1[l1] <= i - k ) ++l1 ;
30         maxx[i]  = sum[q1[l1]] ; 
31         while( l2 <= r2 && sum[q2[r2]] > sum[i] ) --r2 ;
32         q2[++r2] = i ; 
33         while( l2 <= r2 && q2[l2] <= i - k ) ++l2 ;
34         minn[i]  = sum[q2[l2]] ;
35     }
36 }
37 
38 char b[48] ;
39 void print( int x )
40 {
41     //cout<<x<<endl;
42     if( x == 0 ) { putchar(0) ; putchar( ) ; return ; }
43     if( x < 0 ) { putchar( - ) ; x = -x ; }
44     int now = 0 ;
45     while( x ) { b[++now] = x % 10 + 0  ;  x /= 10 ; }
46     while( now ) putchar( b[now--]  ) ;
47     putchar(   ) ;
48 }
49 
50 
51 void Output( )
52 {
53     for( int i = k ; i <= n ; ++i )
54         print( minn[i] ) ;
55     puts( "" ) ;    
56     for( int i = k ; i <= n ; ++i )
57         print( maxx[i] ) ;    
58     puts( "" ) ;    
59 }
60 
61 int main( )
62 {
63 //    freopen( "POJ2823.in" , "r" , stdin ) ;
64 //    freopen( "POJ2823.out" , "w" , stdout ) ;
65     while( ~scanf( "%d%d" , &n , &k ) )
66     {
67         Init( ) ;
68         Solve( ) ;
69         Output( ) ;
70     }
71     fclose( stdin ) ;
72     fclose( stdout ) ;
73     return 0 ; 
74 }

 



以上是关于POJ#2823. Sliding Window的主要内容,如果未能解决你的问题,请参考以下文章

POJ 2823 Sliding Window

POJ 2823 Sliding Window

POJ2823 Sliding Window

poj 2823 Sliding Window 题解

POJ#2823. Sliding Window

poj 2823 Sliding Window