Codeforces 371C Hamburgers (二分答案)

本文介绍了一个名为'Hamburgers'的问题解决方案,通过二分查找确定最大可制作汉堡数量,并使用贪心策略进行可行性判断。代码实现清晰展示了如何平衡不同汉堡材料的成本与可用数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接 Hamburgers

二分答案,贪心判断即可。

#include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for(int i(0); i <  (n); ++i)
#define LL       long long

char str[1010];
LL len;
LL b, c, s, nb, nc, ns, pb, pc, ps;
LL money;

bool judge(LL x){
    LL mb = x * b;
    LL ms = x * s;
    LL mc = x * c;

    LL now_money = money;
    if (mb > nb) now_money -= (mb - nb) * pb;
    if (ms > ns) now_money -= (ms - ns) * ps;
    if (mc > nc) now_money -= (mc - nc) * pc;

    return now_money >= 0LL;
}

int main(){

    scanf("%s", str);
    len = strlen(str);
    REP(i, len){
        if (str[i] == 'B') ++b;
        else if (str[i] == 'S') ++s;
        else ++c;
    }

    cin >> nb >> ns >> nc;
    cin >> pb >> ps >> pc;
    cin >> money;

    LL l = 0, r = 1e14;
    while (l + 1 < r){
        LL mid = (l + r) / 2;
        if (judge(mid)) l = mid; else r = mid - 1;
    }

    if (judge(r)) cout << r << endl; else cout << l << endl;
    return 0;

}

 

转载于:https://www.cnblogs.com/cxhscst2/p/6359061.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值