From 31e63f59ca9aae2cea05f90c1aebbe0f6bbfc982 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sat, 9 Apr 2022 23:59:11 +0800 Subject: [PATCH] C - Water the Trees https://codeforces.com/contest/1661/submission/153204750 --- Codeforces/1661/C/C.cpp | 52 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Codeforces/1661/C/C.cpp diff --git a/Codeforces/1661/C/C.cpp b/Codeforces/1661/C/C.cpp new file mode 100644 index 00000000..ea178147 --- /dev/null +++ b/Codeforces/1661/C/C.cpp @@ -0,0 +1,52 @@ +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 3e5 + 5; + +int t, n; +long long max, sum, x, a[N], b[N], ans; + +int main() { + std::ios::sync_with_stdio(false); + + cin >> t; + + while (t--) { + ans = std::numeric_limits::max(); + max = 0; + + cin >> n; + for (int i = 1; i <= n; i++) { + cin >> a[i]; + max = std::max(max, a[i]); + } + + for (int _ = 1; _ <= 2; _++, max++) { + sum = x = 0; + + for (int i = 1; i <= n; i++) { + b[i] = max - a[i]; + } + + for (int i = 1; i <= n; i++) { + sum += b[i] / 2 * 2; + x += b[i] % 2; + } + + if (x * 2 > sum) { + ans = std::min(ans, x * 2 - 1); + } else { + sum -= x * 2; + ans = std::min(ans, x * 2 + sum / 3 * 2 + sum % 3); + } + } + + cout << ans << endl; + } + + return 0; +}