Monthly Expense (dichotomy)

------------ ------------ restore content begins

https://blog.csdn.net/u012469987/article/details/50897291 using the binary method to minimize the maximum and minimum values ​​maximized

 

Thinking https://www.cnblogs.com/Sunnie69/p/5423817.html maximum from a single point to two points between the sum of the left border to reduce the interval to ensure the right half section in the right circumstances.

 

#include<iostream>
#include<math.h>
#include<string.h>
#include<string>
#include<stdio.h>
using namespace std;
int a[100005];
int m,n;
bool ok(int x)
{
    int years = 0;
    int res=1;
    for(int i=0; i<m; i++)
    {
        years + = a [i];
        if(ans>x)
        {
            res++;
            years = a [i];
        }
    }
    if(res<=n) return true;
    else return false;
}

int main ()
{
    while(~scanf("%d%d",&m,&n))
    {
        memset(a,0,sizeof(a));
        int maxn=a[0];
        for(int i=0; i<m; i++)
        {
            scanf("%d",&a[i]);
            maxn=max(maxn,a[i]);
        }
        int left=maxn;
        int right=0x7fffffff;
        while(left<right)
        {

            int  mid=left+(right-left)/2;
            //cout<<left<<" "<<mid<<" "<<right<<endl;
            if(ok(mid))
                right=mid;
            else left=mid+1;
        }
        printf("%d\n",left);
    }
}

  

End ------------ ------------ restore content

Guess you like

Origin www.cnblogs.com/RainzzZ/p/12109358.html