C - Monthly Expense

https://vjudge.net/contest/256497#problem/C

最小化最大消费

#include<iostream>
#include<cstdio>
#include<string>
#include<cmath>
#include<vector>
#include<queue>
#include<algorithm>
#include<set>
#include<cstring>
#include<stack>
#include<iomanip>
#define mxan
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
using namespace std;
typedef long long ll;
const int N=2e6+100;
const ll IN=1e18;
const int maxn=1020;
int a[N];
int n,m,high=0,low=-1,mid,hhh;

bool slove(int x)
{
    int res=0,sum=0;
    for(int i=0;i<n;i++)
    {
        sum+=a[i];
        if(sum>x)
        {
            ++res;
            sum=a[i];
        }
    }
    res++;
    if(res>m) return false;

    else return true;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>m;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        if(a[i]>low) low=a[i];
        high+=a[i];
    }
    while(low<high)
    {
        mid=(low+high)/2;
        if(slove(mid))
        {
            high=mid;
        }
        else
            low=mid+1;
    }
    mid = (low+ high ) / 2;
    cout<<mid<<endl;

}
上一篇:Monthly Expense (二分法)


下一篇:外网用户远程控制内部网络设备