From af869f388761ad8903757f0921000f0e7968ef0f Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Wed, 30 Jun 2021 10:52:21 +0800 Subject: [PATCH] =?UTF-8?q?#421.=20=E5=87=A0=E4=BD=95=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E4=B8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://sjzezoj.com/submission/12026 --- S2OJ/421/421.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 S2OJ/421/421.cpp diff --git a/S2OJ/421/421.cpp b/S2OJ/421/421.cpp new file mode 100644 index 00000000..a6f407b4 --- /dev/null +++ b/S2OJ/421/421.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +long long n, m, k, a[100005], sum[100005], ans; + +long long getSum(long long l, long long r) { + long long len = r - l + 1; + if (len % 2 == 0) { + long long mid = l + (len / 2) - 1; + return sum[r] - sum[mid] - (sum[mid] - sum[l - 1]); + } else { + long long mid = l + r >> 1; + return sum[r] - sum[mid] - (sum[mid - 1] - sum[l - 1]); + } + return -1; +} + +bool check(long long x) { + for (long long i = 1; i + x - 1 <= n; i++) { + if (getSum(i, i + x - 1) <= k) { + return true; + } + } + return false; +} + +int main() { + cin >> n >> m >> k; + for (long long i = 1; i <= n; i++) { + cin >> a[i]; + sum[i] = sum[i - 1] + a[i]; + } + long long l = 1, r = n; + while (l <= r) { + long long mid = l + r >> 1; + if (check(mid)) { + ans = mid; + l = mid + 1; + } else { + r = mid - 1; + } + } + cout << ans << endl; + return 0; +}