AcWing 5966. 月度开销
原题链接
简单
作者:
拼凑回忆
,
2025-01-16 21:29:39
,
所有人可见
,
阅读 2
#include <iostream>
using namespace std;
const int N = 1e5 + 10;
int n, m;
int a[N];
bool check(int mid)
{
//sum 代表天数 res 代表当前财政数额
int sum = 1, res = 0;
for (int i = 1; i <= n; i++)
{
if (res + a[i] <= mid)
res += a[i];
else
sum++, res = a[i];
if (sum > m) return false;
}
return true;
}
int main()
{
int l = 1, r = 1e9;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
l = max(l, a[i]);
}
//找左边界
while (l < r)
{
int mid = l + r >> 1;
if (check(mid)) r = mid;
else l = mid + 1;
}
cout << l;
return 0;
}