洛谷题单指南-二叉堆与树状数组-P2251 质量检测
原题链接:https://www.luogu.com.cn/problem/P2251
题意解读:求窗口m内的最小值
解题思路:直接用单调队列求解即可
100分代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
int n, m;
int a[N];
int q[N], head = 0, tail = -1;
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i <= n; i++)
{
while(head <= tail && i - q[head] + 1 > m) head++;
while(head <= tail && a[i] < a[q[tail]]) tail--;
q[++tail] = i;
if(i >= m) cout << a[q[head]] << endl;
}
return 0;
}