Files
python/TangDou/LuoGuBook/P1182.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

45 lines
930 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
}