Saturday, May 16, 2015

UVa 12390 - Distributing Ballot Boxes

// UVa 12390 - Distributing Ballot Boxes

#include <iostream>
#include <stdio.h>
#include <queue>
using namespace std;

int a[500000];
int n, b;

bool possible(int m) {
	int tot = 0;
	for (int i = 0; i < n; i++) {
		int need = (a[i] / m) + (a[i] % m ? 1 : 0);
		tot += need;
	}
	return tot <= b;
}

int main() {

	while ((cin >> n >> b) && (n != -1 || b != -1)) {
		int fin = 0;
		for (int i = 0; i < n; i++) {
			scanf("%d", &a[i]);
			if (a[i] > fin)
				fin = a[i];
		}
		int sol = 0;
		int ini = 1;
		while (ini <= fin) {
			int mid = (ini + fin) >> 1;
			if (possible(mid)) {
				sol = mid;
				fin = mid - 1;
			} else
				ini = mid + 1;
		}
		cout << sol << endl;
	}
	return 0;
}

No comments:

Post a Comment