#include <iostream>
#include <algorithm>
#include <deque>
#include <sstream>
#define int int_fast64_t
using namespace std;
int n,m;
signed main() {
cin >> n >> m;
int a[n + 1];
cin>>a[1];
deque<int> mindeq,maxdeq;
mindeq.push_back(1);
maxdeq.push_front(1);
ostringstream min,max;
for (int i = 2; i <= n; i++) {
cin >> a[i];
while (!mindeq.empty()&&i-mindeq.front()>m){
mindeq.pop_front();
}
while (!mindeq.empty()&&a[mindeq.back()]>a[i]){
mindeq.pop_back();
}
if (i>3)min<<a[mindeq.front()]<<' ';
mindeq.push_back(i);
while (!maxdeq.empty()&&i-maxdeq.front()>m){
maxdeq.pop_front();
}
while (!maxdeq.empty()&&a[maxdeq.back()]<a[i]){
maxdeq.pop_back();
}
if (i>3)max<<a[maxdeq.front()]<<' ';
maxdeq.push_back(i);
}
cout<<min.str()<<*min_element(a+n-3,a+n+1)<<endl<<max.str()<<*max_element(a+n-3,a+n+1);
return 0;
}
题目 评测记录