![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/06433d512171c22bbd2750c5fd612cdc.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;
}