求助,deque怎么看怎么对,下了样例也没看出错哪了,QWQ

P2032 扫描

你想用 cnt 维护什么,我没看懂 但你的问题应该在队首出队的判定上 你应该判定队首的下标,若不在范围内则出队 以下是我推荐的代码写法的片段 ```cpp struct Node { int id,val; } deque <Node> a; while (!a.empty() && x>a.back().val) a.pop_back(); Node now; now.id=i; now.val=x; a.push_back(now); while (a.front().id<=i-k) a.pop_front(); if (i>=k) cout<<a.front().val<<endl; ``` 你可以解释一下 cnt 的用途吗? 如果对上述代码有疑问,可提出
by MTFlowCzq @ 2023-08-05 15:32:40


这题正解不是单调队列吗? ```cpp #include "bits/stdc++.h" using namespace std; const int N=1e6+10; int q[N],h=1,t=0,n,a[N],k; signed main(void) { scanf("%d%d",&n,&k); for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=n;i++) { if(h<=t&&q[h]<i-k+1) h++; while(h<=t&&a[i]>=a[q[t]]) t--; q[++t]=i; if(i>=k) printf("%d\n",a[q[h]]); } return 0; } ```
by zhangjunxian1234 @ 2023-09-23 10:55:51


|