1 solutions

  • 0
    @ 2024-12-11 0:49:57

    C++ :

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    typedef long long ll;
    const int maxn=100001;
    ll n,m,tot,ma;
    ll a[maxn];
    //void work();
    int main()
    {
    	//freopen("fruit.in","r",stdin);
    	//freopen("fruit.out","w",stdout);
    	cin>>n>>m;
    	tot=0;ma=0;
    	for(int i=1;i<=n;i++)
    	{
    		cin>>a[i];
    		if(a[i]>ma) ma=a[i];
    		tot+=a[i];
    	}
    	ll s=ma,t=tot+1;
    	ll mid;
    	while(s<t)
    	{
    		mid=(s+t)>>1;
    		ll v=mid,gs=1;
    		bool flag=0;
    		for(int i=1;i<=n;i++)
    		{
    			if(a[i]<=v) v=v-a[i];
    			else
    			{
    				v=mid-a[i];
    				gs++;
    			}
    			if(gs>m)
    			{
    				s=mid+1;
    				flag=true;
    				break;
    			}
    		}
    		if(!flag) t=mid;
    	//	cout<<s<<' '<<t<<' '<<flag<<endl;
    	}
    	cout<<s<<endl;
    //	while(1);
    	return 0;
    }
    
    
    • 1

    Information

    ID
    771
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By