2022-10-28 14:11:01 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <deque>
|
|
|
|
#include <vector>
|
2020-11-25 11:29:24 +00:00
|
|
|
|
2022-10-28 14:11:01 +00:00
|
|
|
using std::cin;
|
|
|
|
using std::cout;
|
|
|
|
const char endl = '\n';
|
2020-11-25 11:29:24 +00:00
|
|
|
|
2022-10-28 14:11:01 +00:00
|
|
|
const int N = 1e6 + 5;
|
|
|
|
|
|
|
|
int n, k, a[N];
|
|
|
|
std::deque<int> q_min, q_max;
|
|
|
|
std::vector<int> ans_min, ans_max;
|
2020-11-25 11:29:24 +00:00
|
|
|
|
|
|
|
int main() {
|
2022-10-28 14:11:01 +00:00
|
|
|
std::ios::sync_with_stdio(false);
|
|
|
|
cin.tie(nullptr);
|
|
|
|
|
|
|
|
cin >> n >> k;
|
|
|
|
|
2020-11-25 11:29:24 +00:00
|
|
|
for (int i = 1; i <= n; i++) {
|
2022-10-28 14:11:01 +00:00
|
|
|
cin >> a[i];
|
2020-11-25 11:29:24 +00:00
|
|
|
}
|
2022-10-28 14:11:01 +00:00
|
|
|
|
2021-07-02 11:06:57 +00:00
|
|
|
for (int i = 1; i <= n; i++) {
|
|
|
|
while (!q_min.empty() && a[q_min.back()] > a[i]) q_min.pop_back();
|
2022-10-28 14:11:01 +00:00
|
|
|
while (!q_max.empty() && a[q_max.back()] < a[i]) q_max.pop_back();
|
|
|
|
|
|
|
|
q_min.emplace_back(i);
|
|
|
|
q_max.emplace_back(i);
|
|
|
|
|
2021-07-02 11:06:57 +00:00
|
|
|
if (q_min.front() < i - k + 1) q_min.pop_front();
|
|
|
|
if (q_max.front() < i - k + 1) q_max.pop_front();
|
2022-10-28 14:11:01 +00:00
|
|
|
|
2021-07-02 11:06:57 +00:00
|
|
|
if (i >= k) {
|
2022-10-28 14:11:01 +00:00
|
|
|
ans_min.emplace_back(a[q_min.front()]);
|
|
|
|
ans_max.emplace_back(a[q_max.front()]);
|
2021-07-02 11:06:57 +00:00
|
|
|
}
|
2020-12-02 02:09:33 +00:00
|
|
|
}
|
2022-10-28 14:11:01 +00:00
|
|
|
|
|
|
|
for (int x : ans_min) {
|
|
|
|
cout << x << ' ';
|
2020-12-02 02:09:33 +00:00
|
|
|
}
|
2022-10-28 14:11:01 +00:00
|
|
|
|
2021-07-02 11:06:57 +00:00
|
|
|
cout << endl;
|
2022-10-28 14:11:01 +00:00
|
|
|
|
|
|
|
for (int x : ans_max) {
|
|
|
|
cout << x << ' ';
|
2020-11-25 11:29:24 +00:00
|
|
|
}
|
2022-10-28 14:11:01 +00:00
|
|
|
|
2021-07-02 11:06:57 +00:00
|
|
|
cout << endl;
|
2022-10-28 14:11:01 +00:00
|
|
|
|
2020-11-25 11:29:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|