From 24558c0c98ee828d3aadaeb546b503a6b0726e15 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 18 Aug 2022 15:29:13 +0800 Subject: [PATCH] =?UTF-8?q?P1121=20=E7=8E=AF=E7=8A=B6=E6=9C=80=E5=A4=A7?= =?UTF-8?q?=E4=B8=A4=E6=AE=B5=E5=AD=90=E6=AE=B5=E5=92=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/84279869 --- Luogu/P1121/P1121.cpp | 80 ++++++++++++++++++++++++++++++++++++ Luogu/P1121/data/P1121_2.in | 3 ++ Luogu/P1121/data/P1121_2.out | 3 ++ 3 files changed, 86 insertions(+) create mode 100644 Luogu/P1121/P1121.cpp create mode 100644 Luogu/P1121/data/P1121_2.in create mode 100644 Luogu/P1121/data/P1121_2.out diff --git a/Luogu/P1121/P1121.cpp b/Luogu/P1121/P1121.cpp new file mode 100644 index 00000000..1e40489d --- /dev/null +++ b/Luogu/P1121/P1121.cpp @@ -0,0 +1,80 @@ +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 2e5 + 5; + +int n, a[N]; +bool flag = true; +long long sum, s_max[N], s_min[N], f_max[N], f_min[N], g_max[N], g_min[N], ans; + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n; + + for (int i = 1; i <= n; i++) { + cin >> a[i]; + + sum += a[i]; + flag &= a[i] < 0; + } + + if (flag) { + std::sort(a + 1, a + 1 + n); + + cout << a[n] + a[n - 1] << endl; + + exit(0); + } + + ans = std::numeric_limits::min(); + std::fill_n(s_max, N, std::numeric_limits::min()); + std::fill_n(s_min, N, std::numeric_limits::max()); + std::fill_n(f_max, N, std::numeric_limits::min()); + std::fill_n(f_min, N, std::numeric_limits::max()); + std::fill_n(g_max, N, std::numeric_limits::min()); + std::fill_n(g_min, N, std::numeric_limits::max()); + + for (int i = 1; i <= n; i++) { + s_max[i] = std::max(s_max[i - 1] + a[i], static_cast(a[i])); + s_min[i] = std::min(s_min[i - 1] + a[i], static_cast(a[i])); + + f_max[i] = std::max(f_max[i - 1], s_max[i]); + f_min[i] = std::min(f_min[i - 1], s_min[i]); + } + + std::fill_n(s_max, N, std::numeric_limits::min()); + std::fill_n(s_min, N, std::numeric_limits::max()); + + for (int i = n; i; i--) { + s_max[i] = std::max(s_max[i + 1] + a[i], static_cast(a[i])); + s_min[i] = std::min(s_min[i + 1] + a[i], static_cast(a[i])); + + g_max[i] = std::max(g_max[i + 1], s_min[i]); + g_min[i] = std::min(g_min[i + 1], s_min[i]); + } + + std::cerr << ans << endl; + + // 不跨环 + for (int i = 1; i < n; i++) { + ans = std::max(ans, f_max[i] + g_max[i + 1]); + } + + std::cerr << ans << endl; + + // 跨环 + for (int i = 1; i < n; i++) { + ans = std::max(ans, sum - (f_min[i] + g_min[i + 1])); + } + + cout << ans << endl; + + return 0; +} diff --git a/Luogu/P1121/data/P1121_2.in b/Luogu/P1121/data/P1121_2.in new file mode 100644 index 00000000..2e9b6795 --- /dev/null +++ b/Luogu/P1121/data/P1121_2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cedfd09f553d73b477e507dfa61da4d87a9c63e705b1b8bde1955e5707099842 +size 10202 diff --git a/Luogu/P1121/data/P1121_2.out b/Luogu/P1121/data/P1121_2.out new file mode 100644 index 00000000..59b510e2 --- /dev/null +++ b/Luogu/P1121/data/P1121_2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a1f34de2dd4079b3d84921022600aa25a71fe7c6652170eaab5737ae4423dc8f +size 6