POJ-2823 Sliding Window

An array of size n ≤ 10 6 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 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.

Input
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.
Output
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.
Sample Input
8 3
1 3 -1 -3 5 3 6 7

Sample Output
-1 -3 -3 -3 3 3
3 3 5 5 6 7


[ 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

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>

using namespace std;

//const int maxn=1e6+7;
//int stmax[maxn][20],stmin[maxn][20];
//int a[maxn];
//
//void rmq_st(int n){
//    for(int i=1;i<=n;i++)
//        stmax[i][0]=stmin[i][0]=a[i];
//    int m=(int)(double(log(n))/log(2.0));
//    for(int j=1;j<=m;j++)
//    for(int i=1;i+(1<<j)-1<=n;i++){
//        stmax[i][j]=max(stmax[i][j-1],stmax[i+(1<<j-1)][j-1]);
//        stmin[i][j]=min(stmin[i][j-1],stmin[i+(1<<j-1)][j-1]);
//    }
//}
//
//int rmq_maxquery(int l,int r){
//    int k=(int)(double(log(r-l+1))/log(2.0));
//    return max(stmax[l][k],stmax[r-(1<<k)+1][k]);
//}
//int rmq_minquery(int l,int r){
//    int k=(int)(double(log(r-l+1))/log(2.0));
//    return min(stmin[l][k],stmin[r-(1<<k)+1][k]);
//}
//
//
//int main(){
//    int n,k;
//    ios::sync_with_stdio(false);
//    cin.tie(0);
//    while(cin>>n>>k){
//        for(int i=1;i<=n;i++)
//            cin>>a[i];
//        rmq_st(n);
//        k--;
//        for(int i=1;i+k<n;i++)
//            cout<<rmq_minquery(i,i+k)<<" ";
//        cout<<rmq_minquery(n-k,n)<<endl;
//        for(int i=1;i+k<n;i++)
//            cout<<rmq_maxquery(i,i+k)<<" ";
//        cout<<rmq_maxquery(n-k,n)<<endl;
//
//
//    }
//
//    return 0;
//}

const int maxn=1e6+7;
int a[maxn];
int maxq[maxn];
int minq[maxn];
int q[maxn];
int n,k;

int main(){
ios::sync_with_stdio(false);
cin.tie(0);
while(cin>>n>>k){
for(int i=1;i<=n;i++)
cin>>a[i];
memset(q,0,sizeof(q));
q[tail]=1;
minq[1]=a[1];
for(int i=2;i<=n;i++){
tail--;
q[++tail]=i;
}
memset(q,0,sizeof(q));
q[tail]=1;
maxq[1]=a[1];
for(int i=2;i<=n;i++){
tail--;
q[++tail]=i;
}
for(int i=k;i<n;i++)
cout<<minq[i]<<" ";
cout<<minq[n]<<endl;
for(int i=k;i<n;i++)
cout<<maxq[i]<<" ";
cout<<maxq[n]<<endl;
}
return 0;
}