AcWing 154. 滑动窗口--deque双端队列
原题链接
简单
作者:
会飞的泡泡
,
2021-03-20 20:16:30
,
所有人可见
,
阅读 392
#include <iostream>
#include <queue>
using namespace std;
const int maxn=1e6+10;
typedef long long LL;
int a[maxn];
deque<int> q;
int main(){
int n,k;
cin>>n>>k;
for(int i=0; i<n; i++){
cin>>a[i];
}
for(int i=0; i<n; i++){
if(q.size()&&q.front()<i-k+1)q.pop_front();
while(!q.empty()&&a[i]<=a[q.back()])q.pop_back();
q.push_back(i);
if(i>=k-1)cout<<a[q.front()]<<' ';
}
q.clear();
cout<<endl;
for(int i=0; i<n; i++){
if(q.size()&&q.front()<i-k+1)q.pop_front();
while(!q.empty()&&a[i]>=a[q.back()])q.pop_back();
q.push_back(i);
if(i>=k-1)cout<<a[q.front()]<<' ';
}
return 0;
}