You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
930 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int a[N];
int n, m;
//检查函数
bool check(int mid) {
int cnt = 1; //最小一个区间
int sum = 0; //区间和
for (int i = 1; i <= n; i++) {
//区间和计算
sum += a[i];
//如果超过了mid值
if (sum > mid) {
cnt++; //需要下一个段了~
sum = a[i];
}
}
//是否符合题意的要求在m个区间段范围内
return cnt <= m;
}
int l, r;
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) {
int mid = l + r >> 1; //mid的定义每段和
if (check(mid)) r = mid;//向左逼近
else l = mid + 1;
}
//输出大吉
cout << l << endl;
return 0;
}