poj3273 Monthly Expense(二分)

#include<iostream>
using namespace std;
int money[100005];
int n,m,low=0,high=0;
bool judge(int mid)
{   int sum=0;
    int group=1;
    for(int i=1;i<=n;i++)
    {
        if(sum+money[i]<=mid)
            sum+=money[i];
        else
            {sum=money[i];
            group++;
            }
    }
    if(group>m)
    return false;
    else return true;
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        cin>>money[i];
        high+=money[i];
        if(low<money[i])
            low=money[i];
    }
    int mid=(low+high)>>1;
    while(low<high)
    {   if(!judge(mid))
        low=mid+1;
    else high=mid-1;
    mid=(low+high)>>1;
    }
    cout<<mid<<endl;
    return 0;
}

你可能感兴趣的:(二分)