Codeforces Round #540 (Div. 3) D. Coffee and Coursework 二分

题解

题目大意,有若干杯咖啡,每杯咖啡有一个收益a[i],不限制每天喝多少杯,但是每天的第k杯收益会减少k-1,问总收益大于n的所需最少天数。

使用二分答案求解,每次喝肯定是挑剩余最大的去喝,check时mid天每天都只先喝第一杯如果不够再喝第二杯第三杯。。
在喝的过程中如果当前杯的收益小于等于当前所减少的收益则说明答案不可行。

AC代码

#include <stdio.h>
#include <bits/stdc++.h>
#define fst first
#define sed second
using namespace std;
typedef long long ll;

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int N = 2e5 + 10;
ll a[N];
ll n, m;

bool check(ll mid)
{
	int k = 1;
	ll tot = 0;
	for (int i = 0;; i++) //次
		for (int j = 0; j < mid; j++) //天
		{
			if (k > n || a[k] <= i)
				return 0;
			tot += a[k++] - i;
			if (tot >= m)
				return 1;
		}
}
int main()
{
#ifdef LOCAL
	freopen("C:/input.txt", "r", stdin);
#endif
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		scanf("%I64d", &a[i]);
	sort(a + 1, a + n + 1, greater<ll>());
	ll l = 1, r = m, ans = -1;
	while (l <= r)
	{
		ll mid = l + r >> 1;
		if (check(mid))
			r = mid - 1, ans = mid;
		else
			l = mid + 1;
	}
	cout << ans << endl;

	return 0;
}

猜你喜欢

转载自blog.csdn.net/CaprYang/article/details/87870440