80分求调,#4WA
查看原帖
80分求调,#4WA
1029108
Zhu_PingYuan楼主2024/7/3 19:47
#include <bits/stdc++.h>
using namespace std;
long long n, m, a[100005], ans, Max = 1;
long long cnt;
bool check(int x)
{
	long long t = 1, cnt = 0;
	for(int i = 1; i <= n; i++)
	{
		if(a[i] > x)
			return false;
		if(cnt + a[i] <= x)
			cnt += a[i];
		else
			t++, cnt = a[i];
	}
	return cnt <= x && t >= m;
}
int main()
{
	cin >> n >> m;
	for(int i = 1; i <= n; i++)
		cin >> a[i], cnt += a[i], Max = max(Max, a[i]);
	int l = Max, r = cnt, mid;
	while(l <= r)
	{
		mid = (l + r) / 2;
		if(check(mid))
			l = mid + 1, ans = mid;
		else
			r = mid - 1;
	}
	cout << ans + 1;
	return 0;
} 
2024/7/3 19:47
加载中...