#include <bits/stdc++.h>
#define int long long
using namespace std;
deque <int> minq;
deque <int> maxq;
vector <int> s;
vector <int> mins;
vector <int> maxs;
int n, k;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (int i = 0; i < n; i++){
int x;
cin >> x;
s.push_back(x);
}
for (int i = 0; i < n; i++){
while (!minq.empty() && minq.front() < i - k + 1)
minq.pop_front();
while (!minq.empty() && s[minq.back()] >= s[i])
minq.pop_back();
minq.push_back(i);
while (!maxq.empty() && maxq.front() < i - k + 1)
maxq.pop_front();
while (!maxq.empty() && s[maxq.back()] <= s[i])
maxq.pop_back();
maxq.push_back(i);
if (i >= k - 1) {
mins.push_back(s[minq.front()]);
maxs.push_back(s[maxq.front()]);
}
}
for (auto i : mins)
cout << i << ' ';
cout << '\n';
for (auto i : maxs)
cout << i << ' ';
return 0;
}剪贴板
剪贴板 jxi293vf
剪贴板信息
剪贴板信息
- 收录
- 更新