0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-09-16 20:05:26 +00:00

P1886 滑动窗口 /【模板】单调队列

https://www.luogu.com.cn/record/91964513
This commit is contained in:
Baoshuo Ren 2022-10-28 22:11:01 +08:00
parent e72bb88470
commit 4895da7d09
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

View File

@ -1,35 +1,54 @@
#include <bits/stdc++.h>
#include <iostream>
#include <deque>
#include <vector>
using namespace std;
using std::cin;
using std::cout;
const char endl = '\n';
int n, k, a[1000005];
deque<int> q_min, q_max;
vector<int> ans_min, ans_max;
const int N = 1e6 + 5;
int n, k, a[N];
std::deque<int> q_min, q_max;
std::vector<int> ans_min, ans_max;
int main() {
scanf("%d%d", &n, &k);
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[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);
while (!q_max.empty() && a[q_max.back()] < a[i]) q_max.pop_back();
q_min.emplace_back(i);
q_max.emplace_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()]);
ans_min.emplace_back(a[q_min.front()]);
ans_max.emplace_back(a[q_max.front()]);
}
}
for (int i = 0; i < ans_min.size(); i++) {
cout << ans_min[i] << ' ';
for (int x : ans_min) {
cout << x << ' ';
}
cout << endl;
for (int i = 0; i < ans_max.size(); i++) {
cout << ans_max[i] << ' ';
for (int x : ans_max) {
cout << x << ' ';
}
cout << endl;
return 0;
}