From 88667928c45fa3211e1e04fad1802c16b7691a39 Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Sun, 12 Sep 2021 18:17:37 +0800 Subject: [PATCH] =?UTF-8?q?154.=20=E6=BB=91=E5=8A=A8=E7=AA=97=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.acwing.com/problem/content/submission/code_detail/7685858/ --- AcWing/154/154.cpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 AcWing/154/154.cpp diff --git a/AcWing/154/154.cpp b/AcWing/154/154.cpp new file mode 100644 index 00000000..6c4471f3 --- /dev/null +++ b/AcWing/154/154.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +int n, k, a[1000005]; +deque q_min, q_max; +vector 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; +}