From b425bd8067ff3f92751185e9370a299b67724707 Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Wed, 30 Dec 2020 19:09:01 +0800 Subject: [PATCH] =?UTF-8?q?P1182=20=E6=95=B0=E5=88=97=E5=88=86=E6=AE=B5=20?= =?UTF-8?q?Section=20II?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit R44432005 --- problem/P1182/P1182.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 problem/P1182/P1182.cpp diff --git a/problem/P1182/P1182.cpp b/problem/P1182/P1182.cpp new file mode 100644 index 00000000..cd56d65d --- /dev/null +++ b/problem/P1182/P1182.cpp @@ -0,0 +1,38 @@ +#include + +using namespace std; + +int n, m, a[100005], l, r, mid, ans; + +bool check(int x) { + int tot = 0, num = 0; + for (int i = 1; i <= n; i++) { + if (tot + a[i] <= x) { + tot += a[i]; + } + else { + tot = a[i]; + num++; + } + } + return num >= m; +} +int main() { + cin >> n >> m; + for (int i = 1; i <= n; i++) { + cin >> a[i]; + l = max(l, a[i]); + r += a[i]; + } + while (l <= r) { + mid = l + r >> 1; + if (check(mid)) { + l = mid + 1; + } + else { + r = mid - 1; + } + } + cout << l << endl; + return 0; +} \ No newline at end of file