#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 100005;
int n, m;
int a[N];
int b[N];
bool check(int x)
{
for (int i = 0;i < n;i++) //剪枝,很重要
if (a[i] > x) return false;
memset(b, 0, sizeof b);
int cnt = 1;
for (int i = 0;i < n;i++)
if (b[cnt] + a[i] > x)
{
b[++cnt] = a[i];
}
else
b[cnt] += a[i];
return cnt <= m;
}
int main()
{
cin >> n >> m;
for (int i = 0;i < n;i++)
cin >> a[i];
int l = 0, r = 1e9;
while (l < r)
{
int mid = l + r >> 1;
if (check(mid))
r = mid;
else
l = mid + 1;
}
cout << l;
}
P1182 数列分段 Section II(二分答案+贪心)
最新推荐文章于 2024-08-16 16:00:00 发布