POJ 3273. Monthly Expense

Link
题意:
将长度为n的序列分为连续的m段,使每段和的最大值最小,求这个最小值
思路:
求出序列中的最大值,答案肯定大于等于它
求出序列和,答案肯定小于等于它
二分答案即可
代码:

#include<iostream>
#include<algorithm>

using namespace std;

const int N=1e6+5;

int n,m;
int a[N];

bool check(int x) {
    int sum=0,t=1;
    for(int i=1;i<=n;i++) {
        if(sum+a[i]<=x) {
            sum+=a[i];
        }
        else {
            sum=a[i];
            t++;
        }
    }
    return t<=m;
}
int main() {
    ios::sync_with_stdio(false);
        cin.tie(0);
    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<r) {
        int mid=(l+r)>>1;
        if(check(mid)) r=mid;
        else l=mid+1;
    }
    cout<<l<<endl;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/c4Lnn/p/12332969.html