HOILAI_CEO @ 2022-08-29 13:13:04
#include<bits/stdc++.h>
using namespace std;
const long long maxn=5e7;
long long a[maxn],f[maxn],s[maxn];
long long n,k;
int main(){
cin>>n>>k;
for(int i=1;i<=n;i++){
cin>>a[i];
}
int head=1,tail=0;
f[++tail]=1;
for(int i=2;i<=n;i++){
while(tail>=head&&f[head]<=i-k) head++;
while(tail>=head&&a[i]<=a[f[tail]]) tail--;
f[++tail]=i;
if(i>=k) cout<<a[f[head]]<<" ";
}
cout<<endl;
head=1,tail=0;
s[++tail]=1;
for(int i=2;i<=n;i++){
while(tail>=head&&s[head]<=i-k) head++;
while(tail>=head&&a[i]>=a[s[tail]]) tail--;
s[++tail]=i;
if(i>=k) cout<<a[s[head]]<<" ";
}
return 0;
}
by VectorLi @ 2022-08-29 13:49:09
maxn 应该开
by youthpaul @ 2023-01-27 19:30:53
注意考虑滑动窗口只有1的长度,如果直接从int i=2开始会错过第一个元素的输出