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; +}