NiveusNix @ 2019-07-27 11:00:38
#include<bits/stdc++.h>
using namespace std;
int a[2000010],n,m;
deque <int> q;
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<=m;i++){
if(q.empty()){
q.push_back(i);
continue;
}
while(a[i]<a[q.back()]){
q.pop_back();
if(q.empty()){
break;
}
}
q.push_back(i);
}
cout<<a[q.front()]<<" ";
for(int i=m+1;i<=n;i++){
if(q.empty()){
q.push_back(i);
continue;
}
if(q.front()<=i-m){
q.pop_front();
}
while(a[i]<a[q.back()]){
q.pop_back();
if(q.empty()){
break;
}
}
q.push_back(i);
cout<<a[q.front()]<<" ";
}
cout<<endl;
q.clear();
for(int i=1;i<=m;i++){
if(q.empty()){
q.push_back(i);
continue;
}
while(a[i]>a[q.back()]){
q.pop_back();
if(q.empty()){
break;
}
}
q.push_back(i);
}
cout<<a[q.front()]<<" ";
for(int i=m+1;i<=n;i++){
if(q.empty()){
q.push_back(i);
continue;
}
if(q.front()<=i-m){
q.pop_front();
}
while(a[i]>a[q.back()]){
q.pop_back();
if(q.empty()){
break;
}
}
q.push_back(i);
cout<<a[q.front()]<<" ";
}
}