题意:做一个汉堡包需要B,S,C三种材料,给出原来各有多少材料和购买这些材料的单价,有n金币,求最多做多少汉堡包。
二分答案,去一个足够大的数作为上界,0为下界,每次Judge判断满不满足,满足则继续向上。
#include <iostream>
#include <cstdio>
#include<cstring>
#include <cstdlib>
#include <algorithm>
typedef long long ll;
using namespace std;
char str[150];
ll b, s, c, n1, n2, n3, p1, p2, p3, n;
ll max(ll x, ll y)
{
return x > y ? x : y;
}
bool Judge(ll x)
{
ll nb = max((x*b - n1)*p1, 0);
ll ns = max((x*s - n2)*p2, 0);
ll nc = max((x*c - n3)*p3, 0);
return nb+ns+nc > n; // 花费是否比拥有金币大
}
int main()
{
while(~scanf("%s", str)) {
scanf("%lld%lld%lld%lld%lld%lld%lld", &n1, &n2, &n3, &p1, &p2, &p3, &n);
b = 0, s = 0, c = 0;
for(int i = 0; i < strlen(str); i++) {
if(str[i] == 'B') b++;
else if(str[i] == 'S') s++;
else c++;
}
ll mid, l = 0, r = 1e15;
while(r >= l) {
mid = (l+r) / 2;
if(Judge(mid)) r = mid - 1;
else l = mid + 1;
}
printf("%lld\n", r);
}
return 0;
}