0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-10 08:58:48 +00:00

P1886 滑动窗口 /【模板】单调队列

R42580010
This commit is contained in:
Baoshuo Ren 2020-11-25 19:29:24 +08:00 committed by Baoshuo Ren
parent 5c72a7017e
commit 3ed44dbe12
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

37
problem/P1886/P1886.cpp Normal file
View File

@ -0,0 +1,37 @@
#include <bits/stdc++.h>
using namespace std;
struct node {
int key, val;
} a[1000005];
int n, k;
deque<node> q;
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
a[i].key = i;
cin >> a[i].val;
}
// 最小值
for (int i = 1; i <= n; i++) {
while (!q.empty() && a[i].val <= q.front().val) q.pop_front();
q.push_front(a[i]);
while (q.back().key <= i - k) q.pop_back();
if (i >= k) cout << q.back().val << ' ';
}
q.clear();
cout << endl;
// 最大值
for (int i = 1; i <= n; i++) {
while (!q.empty() && a[i].val >= q.front().val) q.pop_front();
q.push_front(a[i]);
while (q.back().key <= i - k) q.pop_back();
if (i >= k) cout << q.back().val << ' ';
}
q.clear();
cout << endl;
return 0;
}