{HDU}{4190}{Distributing Ballot Boxes}{二分答案}

二分答案与箱子数目进行匹配判定。

#include <iostream>

#include <string>

#include <cstring>

#include <cstdio>

#include <algorithm>

#include <memory>

#include <cmath>

#include <bitset>

#include <queue>

#include <vector>

#include <stack>

using namespace std;



const int MAXN = 600000;



#define CLR(x,y) memset(x,y,sizeof(x))

#define MIN(m,v) (m)<(v)?(m):(v)

#define MAX(m,v) (m)>(v)?(m):(v)

#define ABS(x) ((x)>0?(x):-(x))

#define rep(i,x,y) for(i=x;i<y;++i)





int a[MAXN];

int n,b;



bool judge(const int& vote)

{

    int tmp,i,j;

    int t,cnt;

    cnt = 0;

    rep(i,0,n){

        tmp = a[i];

        while( true ) {

            ++cnt;

            tmp -= vote;

            if( tmp <= 0 )

                break;

        }

        if( cnt > b )

            return false;

    }

    return true;

}

int work()

{

    int i,j,tmp,val;

    int l,r,mid;

    while( scanf("%d%d",&n,&b) ){

        if( n == -1 || b == - 1)

            break;

        rep(i,0,n)

            scanf("%d",&a[i]);

        l = 0;

        r =  5000000;

        while( l < r ){

            mid = (l+r)>>1;

            if(judge(mid))

                r = mid;

            else

                l = mid + 1;

        }

        printf("%d\n",r);

    }

    return 0;

}

int main()

{

    work();

    return 0;

}

 

 

 

你可能感兴趣的:(HDU)