CodeForces 371C - Hamburgers ( 二分搜索 )

题意

做汉堡
先给出一个字符串表示每个汉堡的做法(B:bread, S:sausage, C:cheese)
给出三个值表示BSC分别有多少存货
给出三个值表示商店中BSC分别多少钱一份
给出一个值表示店主现在有多少卢布
问最多能做多少汉堡

思路

二分搜索
但是中间有很多溢出没处理好
用的unsigned long long才水过

AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef unsigned long long ll;
const int maxn = 105;
const ll INF = 0x3f3f3f3f3f3f3f3f;
char str[maxn];
ll nb, ns, nc;
ll pb, ps, pc;
ll b, s, c;
ll r;

bool judge(ll mid)
{
    ll aa = (mid*b-nb)*pb;
    ll bb = (mid*s-ns)*ps;
    ll cc = (mid*c-nc)*pc;
    if(mid*b<=nb) aa = 0;
    if(mid*s<=ns) bb = 0;
    if(mid*c<=nc) cc = 0;
    ll a = aa + bb + cc;
    if( a <= r )
        return true;
    return false;
}

int main()
{
    scanf("%s",str);
    int len = strlen(str);
    b = 0, s = 0, c = 0;
    for( int i = 0; i < len; i++ )
    {
        if( str[i] == 'B' ) b++;
        else if( str[i] == 'S' ) s++;
        else if( str[i] == 'C' ) c++;
    }
    scanf("%I64d%I64d%I64d", &nb, &ns, &nc);
    scanf("%I64d%I64d%I64d", &pb, &ps, &pc);
    scanf("%I64d", &r);
    ll ub = INF;
    ll lb = 0;
    while(ub-lb>1)
    {
        ll mid = (ub+lb)/2;
        if( judge(mid) ) lb = mid;
        else ub = mid;
    }
    printf("%I64d\n", lb);
    return 0;
}
posted @ 2018-07-27 21:09  JinxiSui  阅读(192)  评论(0编辑  收藏  举报