洛谷_P2440 木材加工(尚贤)

题目:https://www.luogu.com.cn/problem/P2440

很明显的二分答案

#include 
#include 
#include 
#define SIZE 100000 + 10
#define ll long long
using namespace std;
ll a[SIZE];

ll Seach(const ll &, const ll &);
ll count(const ll &, const ll &);

int main() {
	freopen("cpp.in", "r", stdin);
	freopen("cpp.out", "w", stdout);
	ll n;
	ll m;
	scanf("%lld%lld", &n, &m);
	for (int i = 1; i <= n; ++i)
		scanf("%lld", &a[i]);
	sort(a + 1, a + n + 1);
	printf("%lld\n", Seach(n, m));
	return 0;
}

ll Seach(const ll &n, const ll &m) {
	ll left = 0, right = a[n];
	while (left != right)
		((count(((left + right + 1) >> 1), n) >= m) ? (left = ((left + right + 1) >> 1)) : (right = ((left + right + 1) >> 1) - 1));
	return left;
}

ll count(const ll &x, const ll &n) {
	ll ans = 0;
	for (int i = 1; i <= n; ++i)
		ans += a[i] / x;
	return ans;
}

你可能感兴趣的:(洛谷_P2440 木材加工(尚贤))