xinxin2022 @ 2024-08-28 12:56:16
#include<bits/stdc++.h>
using namespace std;
int n,k;
deque<int> q,q2;
int a[1000005];
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n>>k;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<k;i++){
while(!q.empty()&&q.back()>=a[i]){
q.pop_back();
q2.pop_back();
}
q.push_back(a[i]);
q2.push_back(i);
}
for(int i=k;i<=n;i++){
if(q2.front()<=i-k){
q.pop_front();
q2.pop_front();
}
while(!q.empty()&&q.back()>a[i]){
q.pop_back();
q2.pop_back();
}
q.push_back(a[i]);
q2.push_back(i);
cout<<q.front()<<" ";
}
cout<<endl;
q.clear();
q2.clear();
for(int i=1;i<k;i++){
while(!q.empty()&&q.back()<=a[i]){
q.pop_back();
q2.pop_back();
}
q.push_back(a[i]);
q2.push_back(i);
}
for(int i=k;i<=n;i++){
if(q2.front()<=i-k){
q.pop_front();
q2.pop_front();
}
while(!q.empty()&&q.back()<=a[i]){
q.pop_back();
q2.pop_back();
}
q.push_back(a[i]);
q2.push_back(i);
cout<<q.front()<<" ";
}
return 0;
}
by yanjiadong @ 2024-08-28 13:03:25
第二十行改成 !q.empty()&&q2.front()<=i-k
by xinxin2022 @ 2024-08-28 13:08:15
@yanjiadong 谢,已关。
此帖洁。