题解:P2032 扫描

很水的单调队列板子题。
在单调队列处理数组最大值的途中当队列大小大于K时输出队列最前端的数即可。

AC代码

#include<bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
#define il inline
#define inf 0x3f3f3f3f

using namespace std;
using ll = long long;
using ull = unsigned long long;

const int N = 2e6+10;

int que[N],a[N],k,n,b = 0,f = 1;

int main(){
	// freopen("test.in","r",stdin);
	// freopen("test.out","w",stdout);
	ios::sync_with_stdio(0),cout.tie(0),cin.tie(0);
    cin>>n>>k;
    for(int i = 1;i <= n;i++)cin>>a[i];
    for(int i = 1;i <= n;i++){
        if(i - que[f] >= k)f++;
        while(f <= b && a[i] >= a[que[b]])b--;
        que[++b] = i;
        if(i >= k)cout<<a[que[f]]<<'\n';
    }
	return 0;
}
posted @ 2025-03-01 15:22  Zheng_iii  阅读(8)  评论(0)    收藏  举报