#include<bits/stdc++.h>
using namespace std;
int n,k;
const int N = 1e6+10;
int a[N];
int main(){
scanf("%d%d",&n,&k);
for(int i = 1;i<=n;i++)scanf("%d",&a[i]);
deque<int>q;
for(int i = 1;i<=n;i++){
while(q.size()&&q.back()>a[i]){
q.pop_back();
}
q.push_back(a[i]);
if(i-k>=1&&q.front()==a[i-k]){
q.pop_front();
}
if(i>=k)printf("%d ",q.front());
}
q.clear();
cout<<'\n';
for(int i = 1;i<=n;i++){
while(q.size()&&q.back()<a[i])q.pop_back();
q.push_back(a[i]);
if(i-k>=1&&q.front()==a[i-k])q.pop_front();
if(i>=k)printf("%d ",q.front());
}
}
单调队列 滑动窗口
最新推荐文章于 2024-09-06 10:38:06 发布