buerbute @ 2024-10-20 10:05:36
最后一点wa了
#include<bits/stdc++.h>
using namespace std;
long long a[10000001],n,k;
deque<int> q;
void ins(int x){
while(!q.empty()&&q.back()>x){
q.pop_back();
}
q.push_back(x);
}
void del(int x){
while(!q.empty()&&q.front()==x){
q.pop_front();
}
}
deque<int> q2;
void ins2(int x){
while(!q2.empty()&&q2.back()<x){
q2.pop_back();
}
q2.push_back(x);
}
void del2(int x){
while(!q2.empty()&&q2.front()==x){
q2.pop_front();
}
}
int main(){
// q.push_front(2);
// q.push_back(3);
// q.pop_back();
// q.front();
// q.back();
cin>>n;
cin>>k;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<=k;i++){
ins(a[i]);
}
for(int i=k+1;i<=n+1;i++){
cout<<q.front()<<" ";
ins(a[i]);
del(a[i-k]);
}
cout<<endl;
for(int i=1;i<=k;i++){
ins2(a[i]);
}
for(int i=k+1;i<=n+1;i++){
cout<<q2.front()<<" ";
ins2(a[i]);
del2(a[i-k]);
}
}
by buerbute @ 2024-10-20 10:06:15
初学单调队列
by bladrrxy @ 2024-10-20 11:11:51
把 del 和 del2 函数里的 while 改成 if
by bladrrxy @ 2024-10-20 11:12:22
因为每次只删一个数