zoukankan      html  css  js  c++  java
  • 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 }


  • 相关阅读:
    视图中使用select a.* 更改了表a的结构 导致读取字段值时发生错位
    sql 压缩文件(rar或zip)
    单击触发jquery.autocomplete的两种方法
    CSS布局时容易出的小错误,导致浏览器不兼容或者各种不显示
    使用备份数据库.bal文件还原正在使用的数据库
    sql outer join
    利用GBK双字节编码突破PHP单引号转义限制进行SQL注入:set names gbk导致的sql注入
    对可操作对象的占用状态、锁定状态、解锁状态的一些方案
    回车转换成Tab
    DataGrid超级连接列
  • 原文地址:https://www.cnblogs.com/Ateisti/p/6050598.html
Copyright © 2011-2022 走看看