CodeForces 1084B Kvass and the Fair Nut

版权声明:博主的博客不值钱随便转载但要注明出处 https://blog.csdn.net/easylovecsdn/article/details/85004075

题意:

给你n个瓶子,每个瓶子里有vi升饮料,现在又给你一个桶,让你在尽可能让装有最少饮料瓶子中的饮料多的情况下装满桶,若不能输出-1.

思路:
先选出装有最少饮料的瓶子,先让其他瓶子去填装桶,且其他瓶子填装时所剩余饮料不能少于最少瓶子中的升数,若其他瓶子能将桶填满,输出最小瓶子升数,若不能(此时所有瓶子饮料数相同)再一同填补。

Code:
 

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
const int maxn = 1005;
LL s;
int n, a[maxn];

int main()
{
	cin >> n >> s;
	//n = 1000;
	//s = 980103855476;
	LL sum = 0;

	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		//a[i] = 1000000000;
		if (sum <= s + 1) sum += a[i];
	}
	if (sum == s) cout << 0 << endl;
	else if (sum < s) cout << -1 << endl;
	else
	{
		sort(a, a + n);
		LL pour = 0;
		for (int i = 1; i < n; i++)
		{
			pour += (a[i] - a[0]);
			if (pour >= s) break;
		}
		if (pour >= s) cout << a[0] << endl;
		else
		{
			//cout << pour << endl;
			LL r = s - pour;
			//cout << r << endl;
			int t = r % n == 0 ? (r / n) : (r / n + 1);
			a[0] -= t;
			if (a[0] <= 0) cout << 0 << endl;
			else cout << a[0] << endl;
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/easylovecsdn/article/details/85004075
今日推荐