• 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 }


  • 相关阅读:
    Python——在for循环中更改list值的方法
    治理网络文学盗版可用“大数据疗法”
    脉冲星导航
    Python——if多个条件并列_If有多个条件怎么办?
    对接种疫苗后出现的异常反应,判断是否和疫苗接种相关?进行分析和判断
    Python——OpenCV计算机视觉库,功能介绍和应用
    人脸识别技术、仿真头套、全息投影、人脸跟踪
    Python——项目开发中常用的精华Python库介绍与整理.
    区块链技术在智能时代有着广泛的应用前景
    机器学习——“防干扰训练”《全新算法助机器学习抵抗干扰》
  • 原文地址:https://www.cnblogs.com/Ateisti/p/6050598.html
Copyright © 2020-2023  润新知