0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-14 18:58:47 +00:00

P1182 数列分段 Section II

R44432005
This commit is contained in:
Baoshuo Ren 2020-12-30 19:09:01 +08:00 committed by Baoshuo Ren
parent 20dbd53f88
commit b425bd8067
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

38
problem/P1182/P1182.cpp Normal file
View File

@ -0,0 +1,38 @@
#include <bits/stdc++.h>
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;
}