二分
2024-07-30 13:57:47
发布于:广东
3阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+6;
int n, m, a[N], l=0, r=0;
int main()
{
cin >> n >> m;
for (int i=1; i<=n; i++) {
cin >> a[i];
if (l<a[i]) l = a[i];
r += a[i];
}
while (l<=r) {
int mid = l + (r-l)/2;
int count = 1, tmp=0;
for (int i=1; i<=n; i++) {
if (a[i]+tmp<=mid) tmp += a[i];
else {
count++;
tmp = a[i];
}
}
if (count<=m) r= mid-1;
else l = mid+1;
}
cout<<l;
return 0;
}
这里空空如也
有帮助,赞一个