0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-09-20 10:45:25 +00:00
OI-codes/problem/P1886/P1886.cpp

38 lines
839 B
C++
Raw Normal View History

#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;
}