
二分搜、貪心
#include<bits/stdc++.h>
#define int long long
#define double long double
#define f first
#define s second
#define fastio ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
int n,k;
vector<int>a;
bool check(int m){
int cur = 0, cnt = 1;
for(int i=0;i<n;i++){
if(cur + a[i] > m){
cnt++;
cur = 0;
}
cur += a[i];
}
return cnt <= k;
}
signed main(){
fastio;
cin>>n>>k;
int sm = 0;
int maxx = -1e18;
for(int i=0;i<n;i++){
int x; cin>>x;
a.push_back(x);
sm += x;
maxx = max(maxx,x);
}
int L = maxx-1, R = sm+1;
while(R-L > 1){
int m = L+(R-L)/2;
if(check(m)) R = m;
else L = m;
}
cout<<R<<"\n";
return 0;
}

發佈留言