0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-09-16 20:25:24 +00:00
Baoshuo Ren 2021-09-12 18:17:37 +08:00 committed by Baoshuo Ren
parent b85894c1ec
commit 88667928c4
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

35
AcWing/154/154.cpp Normal file
View File

@ -0,0 +1,35 @@
#include <bits/stdc++.h>
using namespace std;
int n, k, a[1000005];
deque<int> q_min, q_max;
vector<int> ans_min, ans_max;
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
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()]);
}
}
for (int i = 0; i < ans_min.size(); i++) {
cout << ans_min[i] << ' ';
}
cout << endl;
for (int i = 0; i < ans_max.size(); i++) {
cout << ans_max[i] << ' ';
}
cout << endl;
return 0;
}