mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-12-24 18:11:59 +00:00
P1886 滑动窗口 /【模板】单调队列
R42580010
This commit is contained in:
parent
5c72a7017e
commit
3ed44dbe12
37
problem/P1886/P1886.cpp
Normal file
37
problem/P1886/P1886.cpp
Normal 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user