From 4895da7d09fba6529feff8c1a058757e565c2a4f Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Fri, 28 Oct 2022 22:11:01 +0800 Subject: [PATCH] =?UTF-8?q?P1886=20=E6=BB=91=E5=8A=A8=E7=AA=97=E5=8F=A3=20?= =?UTF-8?q?/=E3=80=90=E6=A8=A1=E6=9D=BF=E3=80=91=E5=8D=95=E8=B0=83?= =?UTF-8?q?=E9=98=9F=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/91964513 --- Luogu/P1886/P1886.cpp | 51 +++++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/Luogu/P1886/P1886.cpp b/Luogu/P1886/P1886.cpp index 10056389..43911d5a 100644 --- a/Luogu/P1886/P1886.cpp +++ b/Luogu/P1886/P1886.cpp @@ -1,35 +1,54 @@ -#include +#include +#include +#include -using namespace std; +using std::cin; +using std::cout; +const char endl = '\n'; -int n, k, a[1000005]; -deque q_min, q_max; -vector ans_min, ans_max; +const int N = 1e6 + 5; + +int n, k, a[N]; +std::deque q_min, q_max; +std::vector 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; }