From 99841026f7925306d66682868a0826f22d1cdfa9 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sun, 24 Jul 2022 17:48:12 +0800 Subject: [PATCH] =?UTF-8?q?P8446=20=E8=99=B9=E8=89=B2=E7=9A=84=E5=8C=97?= =?UTF-8?q?=E6=96=97=E4=B8=83=E6=98=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/80990615 --- Luogu/P8446/P8446.cpp | 63 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 Luogu/P8446/P8446.cpp diff --git a/Luogu/P8446/P8446.cpp b/Luogu/P8446/P8446.cpp new file mode 100644 index 00000000..c2a5aa02 --- /dev/null +++ b/Luogu/P8446/P8446.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +#include + +const int N = 4e6 + 5; + +template +void read(T &x) { + x = 0; + T sig = 1; + char c = getchar(); + + for (; !isdigit(c); c = getchar()) { + if (c == '-') sig = -1; + } + + for (; isdigit(c); c = getchar()) { + x = (x << 1) + (x << 3) + c - '0'; + } + + x *= sig; +} + +int n, a[N]; +long long ans_min[N], ans_max[N], ans = -1; +std::stack st_min, st_max; + +int main() { + read(n); + + for (int i = 1; i <= n; i++) { + read(a[i]); + } + + memset(ans_min, 0x3f, sizeof(ans_min)); + + for (int i = 1; i <= n; i++) { + while (!st_max.empty() && a[st_max.top()] > a[i]) { + auto x = ans_max[st_max.top()]; + ans = std::max(ans, x - a[i] - i - 1); + ans_max[i] = std::max(ans_max[i], x); + st_max.pop(); + } + st_max.push(i); + ans_max[i] = std::max(ans_max[i], static_cast(a[i]) + i); + } + + for (int i = 1; i <= n; i++) { + while (!st_min.empty() && a[st_min.top()] < a[i]) { + auto x = ans_min[st_min.top()]; + ans = std::max(ans, static_cast(a[i]) - i - x - 1); + ans_min[i] = std::min(ans_min[i], x); + st_min.pop(); + } + st_min.push(i); + ans_min[i] = std::min(ans_min[i], static_cast(a[i]) - i); + } + + printf("%lld\n", ans); + + return 0; +}