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 }


  • 相关阅读:
    对 Sea.js 进行配置 seajs.config
    jquery 设置style:display
    Js获取当前日期时间及其它操作
    2.4 js数组与字符串的转换 > 我的程序猿之路:第十四章
    2.3 js刷新页面所有 > 我的程序猿之路:第十三章
    2.2 HTML/JSP中控制按钮的显示和隐藏与单页面多列表 > 我的程序猿之路:第十二章
    2.1 字符串替换字符或字符设置为空 > 我的程序猿之路:第十一章
    1.9 23种设计模式之单例模式详情 > 我的程序猿之路:第九章
    1.8 Oracle 登陆时报错信息:无监听程序 > 我的程序猿之路:第八章
    1.7 Oracle 11g )impdp(数据泵)--导入dmp文件(全过程) > 我的程序猿之路:第七章
  • 原文地址:https://www.cnblogs.com/Ateisti/p/6050598.html
Copyright © 2011-2022 走看看