![在这里插入图片描述](https://img-blog.csdnimg.cn/878cb4f4e74d4c3d8a011258d691c698.png)
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 1e6 + 5;
const int mod = 1e9 + 7;
#define pai acos(-1)
int a[maxn], q[maxn];
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
t = 1;
while (t--) {
int n,k;
cin >> n>>k;
for (int i = 1; i <= n; i++)
cin >> a[i];
int h=0,t=-1;
for(int i=1;i<=n;i++)
{
if(h<=t&&q[h]<i-k+1) h++;
while(h<=t&&a[i]<=a[q[t]]) t--;
q[++t]=i;
if(i>k-1) cout<<a[q[h]]<<" ";
}
cout<<endl;
t=-1,h=0;
for(int i=1;i<=n;i++)
{
if(h<=t&&q[h]<i-k+1) h++;
while(h<=t&&a[i]>=a[q[t]]) t--;
q[++t]=i;
if(i>k-1) cout<<a[q[h]]<<" ";
}
}
return 0;
}