codeforces 371C.Hamburgers(二分模板题)

在这里插入图片描述

#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define int long long
using namespace std;
int b, s, c;//B S C的数量
int nb, ns, nc;//原有的B S C材料的数量
int pb, ps, pc;//B S C的单价
int sum;
bool check(int mid)
{
	int total = 0;
	if (mid * b > nb)
		total = total + (mid * b - nb) * pb;
	if (mid * s > ns)
		total = total + (mid * s - ns) * ps;
	if (mid * c > nc)
		total = total + (mid * c - nc) * pc;
	if (total > sum)
		return true;
	else
		return false;
}
signed main()
{
	IOS;
	string str;
	cin >> str;
	b = 0, s = 0, c = 0;
	for (int i = 0; i < str.length(); i++)
	{
		if (str[i] == 'B') b++;
		if (str[i] == 'S') s++;
		if (str[i] == 'C') c++;
	}

	cin >> nb >> ns >> nc >> pb >> ps >> pc >> sum;
	int l = 0, r = 1e13;//r开1e12+5不行
	int mid;
	while (r - l > 1)//整数
	{
		mid = l + r >> 1;
		if (check(mid))
			r = mid;
		else
			l = mid;
	}
	printf("%lld\n", l);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_46272108/article/details/108010626