Sequence segments II

https://loj.ac/problem/10014

Title Description

  A given length N is a positive integer number of columns A, it requires continuous into M segments, each segment worth maximum value of minimum requirements

Thinking

  The most value most in value, typically half the problem. Binary answer at the answer is determined whether the number of the column A can be divided into M segments.

Code

#include <bits/stdc++.h>
using namespace std;
int m,n;
int a[101000];
bool check(int x)
{
    int s=0,sum=1;
    for(int i=1;i<=n;i++)
    {
        if(a[i]>x)return 0;
        s+=a[i];
        if(s>x)
        {
            s=a[i];
            sum++;
        }
    }
    return sum<=m;
}
int main() 
{
//    freopen("aa.txt","r",stdin);
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    int l=0,r=1e9,ans;
    while(l<r)
    {
        int mid=(r+l)>>1;
//        cout<<r<<' '<<check(mid)<<' '<<l<<endl;
        if(check(mid))r=mid;
        else l=mid+1;
    }
    printf("%d",r);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/fangbozhen/p/11609664.html