kuangbin帶你飛 【二分】HDU - 4190 Distributin Ballot Boxes(整數二分)

渲染明天。發表於2020-11-02

在這裡插入圖片描述
題解:待更新
程式碼如下:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<algorithm>

#define ll long long
#define int ll
#define inf (1<<31)-1//注意上界
#define PI acos(-1.0)
using namespace std;
int read() {
	int w = 1, s = 0;
	char ch = getchar();
	while (ch < '0' || ch>'9') { if (ch == '-') w = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0';    ch = getchar(); }
	return s * w;
}
int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }
int ksm(int a, int b, int mod) { int s = 1; while(b) {if(b&1) s=s*a%mod;a=a*a%mod;b>>=1;}return s;}
const int N = 500010;
int a[N];
int n, b;
bool check(int x) {
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum += a[i] / x;
        if (a[i] % x) sum++;
    }
    return sum > b;
}
signed main()
{
	while (~scanf("%lld%lld", &n, &b)) {
        if (n == -1 && b == -1) break;
        memset(a, 0, sizeof(a));
        for (int i = 0; i < n; i++) a[i] = read();
        //二分模板
        int l = 0, r = inf;
        int mid = 0;
        while (l <= r) {
            int sum = 0;
            mid = l + r >> 1;
            if (check(mid)) l = mid + 1;
            else r = mid - 1;
        }
        printf("%lld\n", l);
    }
    return 0;
}

相關文章