标签:span g++ 简单 char print frame val which first
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.
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 }
标签:span g++ 简单 char print frame val which first
原文地址:http://www.cnblogs.com/Ateisti/p/6050598.html