Loading

HDU-4190 Distributing Ballot Boxes

Distributing Ballot Boxes

二分答案

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
const int maxn = 5e5 + 10;
int n, m, num[maxn];

ll judge(int x)
{
    ll ans = 0;
    for (int i = 0; i < n; i++)
        ans += (num[i] + x - 1) / x;
    return ans;
}

int main()
{
    while (scanf("%d%d", &n, &m) != EOF)
    {
        if (n == -1 && m == -1)
            break;
        for (int i = 0; i < n; i++)
            scanf("%d", &num[i]);
        int l = 1, r = 5e6;
        while (r > l)
        {
            int mid = l + r >> 1;
            if (judge(mid) <= m)
                r = mid;
            else
                l = mid + 1;
        }
        printf("%d\n", l);
    }
    return 0;
}
posted @ 2022-04-26 13:30  dgsvygd  阅读(18)  评论(0编辑  收藏  举报