2020-11-25 11:29:24 +00:00
|
|
|
#include <bits/stdc++.h>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2020-12-02 02:09:33 +00:00
|
|
|
int n, k, a[1000005];
|
2021-07-02 11:06:57 +00:00
|
|
|
deque<int> q_min, q_max;
|
|
|
|
vector<int> ans_min, ans_max;
|
2020-11-25 11:29:24 +00:00
|
|
|
|
|
|
|
int main() {
|
2020-12-02 02:09:33 +00:00
|
|
|
scanf("%d%d", &n, &k);
|
2020-11-25 11:29:24 +00:00
|
|
|
for (int i = 1; i <= n; i++) {
|
2020-12-02 02:09:33 +00:00
|
|
|
scanf("%d", &a[i]);
|
2020-11-25 11:29:24 +00:00
|
|
|
}
|
2021-07-02 11:06:57 +00:00
|
|
|
for (int i = 1; i <= n; i++) {
|
|
|
|
while (!q_max.empty() && a[q_max.back()] < a[i]) q_max.pop_back();
|
|
|
|
while (!q_min.empty() && a[q_min.back()] > a[i]) q_min.pop_back();
|
|
|
|
q_max.push_back(i);
|
|
|
|
q_min.push_back(i);
|
|
|
|
if (q_min.front() < i - k + 1) q_min.pop_front();
|
|
|
|
if (q_max.front() < i - k + 1) q_max.pop_front();
|
|
|
|
if (i >= k) {
|
|
|
|
ans_max.push_back(a[q_max.front()]);
|
|
|
|
ans_min.push_back(a[q_min.front()]);
|
|
|
|
}
|
2020-12-02 02:09:33 +00:00
|
|
|
}
|
|
|
|
for (int i = 0; i < ans_min.size(); i++) {
|
2021-07-02 11:06:57 +00:00
|
|
|
cout << ans_min[i] << ' ';
|
2020-12-02 02:09:33 +00:00
|
|
|
}
|
2021-07-02 11:06:57 +00:00
|
|
|
cout << endl;
|
2020-12-02 02:09:33 +00:00
|
|
|
for (int i = 0; i < ans_max.size(); i++) {
|
2021-07-02 11:06:57 +00:00
|
|
|
cout << ans_max[i] << ' ';
|
2020-11-25 11:29:24 +00:00
|
|
|
}
|
2021-07-02 11:06:57 +00:00
|
|
|
cout << endl;
|
2020-11-25 11:29:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|