80分,最后一个wa了,大佬求调!!!!
查看原帖
80分,最后一个wa了,大佬求调!!!!
701120
mab123456楼主2024/12/5 21:59
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const ll N=1e5+5;
ll a[N];
ll n,m,l,r;
bool check(int x)
{
	ll cnt=1,k=x;
	for(int i=1;i<=n;i++)
	{
		if(k>=a[i])
		{
			k-=a[i];
		}
		else
		{
			cnt++;
			k=x-a[i];
		}
	}
	if(cnt>m)return true;
	return false;
}
int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];l=max(l,a[i]);
		r+=a[i];
	}
	while(l+1!=r)
	{
		ll mid=l+r>>1;
		if(check(mid))
		l=mid;
		else
		r=mid;
	}
	if(check(l))
	cout<<r<<endl;
	else
	cout<<l<<endl;
	return 0;
}
2024/12/5 21:59
加载中...