题目分析:
- 数据大小
- 把长度为N的数列分为M段,找出每段和的最大值中的最小值
- 二分法:找出答案区间每段和最大值[l,r]
- 答案存在一个分界点,[l,x] 为非可取解,x为最优解,[x + 1,r]为可取解
- 区间左端点为数列的最大值,右端点为数列的和
- 整数二分解题,mid取最小情况(l + r) >> 1;
代码如下:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
#define inf 0x3f3f3f3f
#define eps 1e-6
#define db double
#define ll long long
#define mm(a,x) memset(a,x,sizeof a)
#define pii pair<int,int>
#define pb push_back
#define el endl
#define debug(x) cerr<<#x<<" = "<<x<<endl
#define fgx cerr<<"-------------------------"<<endl
#define shutio ios::sync_with_stdio(false),cin.tie(0)
#define mk make_pair
#define lowbit(x) (x) & (-x)
#define fi first
#define se second
const int N = 1e5 + 10;
ll n,m;
ll a[N];
ll l,r;
bool check(int x){
ll tot = 1;
ll s = 0;
for(int i = 0; i < n; i ++ ){
if(s + a[i] > x){
s = 0;
tot ++;
}
s += a[i];
}
return tot <= m;
}
int main() {
shutio;
cin >> n >> m;
for(int i = 0; i < n; i ++ ){
cin >> a[i];
l = max(l,a[i]);
r += a[i];
}
while(l < r){
int mid = (l + r) >> 1;
if(check(mid)) r = mid;
else l = mid + 1;
}
cout<<l;
return 0;
}