A_R_O_N_A @ 2023-12-06 13:32:24
WA on #1,3,4,5,10
#include<bits/stdc++.h>
#define ll long long
using namespace std;
mt19937 myrand(time(0));
inline ll read(){
ll x=0,w=1;
char ch=0;
while(ch<'0'||ch>'9'){
if(ch=='-')w=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=x*10+(ch-'0');
ch=getchar();
}
return x*w;
}
void write(ll x){
if(x<0){
putchar('-');
x=-x;
}
static int sta[35];
int top=0;
do{
sta[top++]=x%10,x/=10;
}while(x);
while(top)putchar(sta[--top]+'0');
putchar('\n');
}
deque<ll>q;
int n,m,a[2000005];
main(){
n=read();m=read();
for(int i=1;i<=n;i++)a[i]=read();
for(int i=1;i<=n;i++){
if(q.empty())write(0);
else write(q.back());
if(q.size()>=m)q.pop_back();
while(a[i]<q.front()&&!q.empty())q.pop_front();
q.push_front(a[i]);
}
return 0;
}
by Creeper_l @ 2023-12-06 14:41:13
q 里面应该存下标而不是值,不然你怎么判断有没有
by emo_male_god @ 2023-12-06 14:44:08
@xie_T34 你用队列记录下每个数的下标。
然后这一句:if(q.size()>=m)q.pop_back();
改为 if (i - q.back() + 1 > m) q.pop_back()
如果你只用队列长度 pop,那么在有些时候队列长度小于 m,但是某些数却不在 i - m ~ i之中。
by A_R_O_N_A @ 2023-12-07 12:53:06
thx,此贴结