题目大意:给你N个数字,要起将这N个数字划分成K个区间,并要求每个区间的和的最大值最小
解题思路:最大值最小问题,二分解决
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 1000010;
LL v[N];
LL l, r;
int n, m, cas = 1;
void init() {
scanf("%d%d", &n, &m);
l = r = 0;
for (int i = 0; i < n; i++) {
scanf("%lld", &v[i]);
l = max(v[i], l);
r += v[i];
}
v[n] = 0;
}
bool judge(LL mid) {
LL sum = 0;
int num = 1;
for (int i = 0; i <= n; i++) {
if (sum + v[i] > mid) {
sum = v[i];
num++;
}
else sum += v[i];
}
return num <= m;
}
void solve() {
while (l <= r) {
LL mid = (l + r) >> 1;
if (judge(mid)) r = mid - 1;
else l = mid + 1;
}
printf("Case %d: %lld\n", cas++, r + 1);
}
int main() {
int test;
scanf("%d", &test);
while (test--) {
init();
solve();
}
return 0;
}