HDU 4390 Number Sequence 二分

题目地址:  http://acm.hdu.edu.cn/showproblem.php?pid=4390



#include<iostream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<ctime>
using namespace std;

typedef long long LL;
const int N=500100;

int x[N];
int n,m;

bool test(int k)
{
    int i=1,t=0;
    while(i<=n)
    {
        if(x[i]%k==0)
            t+=(x[i])/k;
        else
            t+=(x[i])/k+1;
        i++;
    }
    if(t<=m) return false;//如果小于等于原有的箱子,说明数字还可以减小,因为减小了数字箱子就会增多
    return true;
}

int main()
{
    int i;
    while(cin>>n>>m&&n+m!=-2)
    {
        int max1=0;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&x[i]);
            if(max1<x[i])
                max1=x[i];
        }
        int l=1,r=max1,mi;
        while(r>l)
        {
            mi=(r+l)/2;
            if(test(mi))//这个地方要特别注意,不要弄反了,实在不行的话,两个都试试
                l=mi+1;
            else
                r=mi;
        }
        printf("%d\n",r);
    }
    return 0;
}


原文地址:https://www.cnblogs.com/javawebsoa/p/3047809.html