poj 3273(二分。。。很水。。。。)

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxM=100005;
int a[maxM],n,m;
inline int cal(int v){
    int i,j,sum=1;
    for(i=0,j=0;i<n;i++){
        j+=a[i];
        if(j>v){
            j=a[i],sum+=1;
        }
    }
    if(sum>m) return 1;
    return 0;
}
int main(){
    int lef,rig,mid,i,u;
    while(scanf("%d%d",&n,&m)!=-1){
        for(i=0,lef=-1,rig=0;i<n;i++){
            scanf("%d",&a[i]);
            if(lef<a[i]) lef=a[i];
            rig+=a[i];
        }
        while(lef<=rig){
            mid=(lef+rig)/2;
            u=cal(mid);
            if(u>0) lef=mid+1;
            else rig=mid-1;
        }
        printf("%d\n",mid);
    }
    return 0;
}


你可能感兴趣的:(poj 3273(二分。。。很水。。。。))