洛谷 P1873 砍树

砍树

二分答案,难度较低。

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
//Mystery_Sky
//
#define M 10000100
#define ll long long 
#define INF 0x7f7f7f7f
ll l, r, mid;
int n, m, a[M];
inline bool check(ll ans)
{
	ll sum = 0;
	for(int i = 1; i <= n; i++) {
		if(a[i] > ans)	sum += (a[i] - ans);
	}
	return sum >= m;
}

int main() {
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
	l = 0, r = INF;
	while(l < r) {
		mid = (l + r + 1) / 2;
		if(check(mid)) l = mid;
		else r = mid - 1;
	}
	printf("%lld\n", l);
	return 0;
}
posted @ 2019-05-16 20:05  Mystery_Sky  阅读(131)  评论(0编辑  收藏  举报