80分求助大佬
查看原帖
80分求助大佬
905810
iridescent_black楼主2023/9/12 20:16
#include <bits/stdc++.h>
#define int long long
#define ULL unsigned long long
#define endl "\n"
#define fi first 
#define se second 
using namespace std;
typedef long long LL;
typedef pair<int,int>PII;
typedef pair<double,double>PDD;
const int N=1e5+10;
int a[N];
int n,m;
bool check(int mid){
    int res=0,cnt=1;
    for(int i = 1;i <= n;i ++){
        if(res+a[i]>mid){
            res=a[i];
            cnt++;
        }
        else res+=a[i];
    }
    if(cnt <= m) return 1;
    else return 0;
}
void solve(){
    cin>>n>>m;
    int l=0,r=0;
    for(int i =1 ;i <= n;i ++) {
        cin>>a[i];
        l=max(l,a[i]);
        r+=a[i];
    }
    
    while(l+1<r){
        int mid=(l+r)>>1;
        if(check(mid)) r=mid;
        else l=mid;
    }
    cout<<r<<endl;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
	solve();
}
2023/9/12 20:16
加载中...