1762F(二分)

题目链接·

思路:

这题用到了一下二分的应用,嗨,当时确实没有想到。可知k是递增的关系。二分判断就可。

AC代码:

#include <bits/stdc++.h>
#define int long long 
using namespace std;
const int N = 2e5 + 10;
int a[N];
int n, c, d;
int sum[N];
bool cmp(int a, int b)
{
	return a > b;
}

bool check(int k)
{
	int sum = 0;
	for (int i = 1; i <= min(n, k + 1); i ++ )
	{
		sum += a[i] * (d / (k + 1) + ((d % (k + 1) + 1) > i));
	}

	return sum >= c;
}

void solved()
{
	cin >> n >> c >> d;
	for (int i = 1; i <= n; i ++ ) cin >> a[i];
	sort(a + 1, a + n + 1, cmp);
	for (int i = 1; i <= n; i ++ ) sum[i] = sum[i - 1] + a[i];
	if (a[1] * d < c)
	{
		cout << "Impossible\n";
		return ;
	}
	if (sum[min(n, d)] >= c)
	{
		cout << "Infinity\n";
		return ;
	}

	
	int l = 0, r = d + 1;
	while (l < r)
	{
		int mid = (l + r + 1) >> 1;
		if(check(mid)) l = mid;
		else r = mid - 1;
	}

	cout << l << '\n';

}

signed main()
{
	ios :: sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int t;
	cin >> t;
	while(t -- )
	{
		solved();
	}
	return 0;
}

 

posted @ 2022-11-22 01:33  Luli&  阅读(28)  评论(0编辑  收藏  举报