Hamburgers CodeForces – 371C(二分答案)

Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own hands. Polycarpus thinks that there are only three decent ingredients to make hamburgers from: a bread, sausage and cheese. He writes down the recipe of his favorite “Le Hamburger de Polycarpus” as a string of letters ‘B’ (bread), ‘S’ (sausage) и ‘C’ (cheese). The ingredients in the recipe go from bottom to top, for example, recipe “ВSCBS” represents the hamburger where the ingredients go from bottom to top as bread, sausage, cheese, bread and sausage again.

Polycarpus has nb pieces of bread, ns pieces of sausage and nc pieces of cheese in the kitchen. Besides, the shop nearby has all three ingredients, the prices are pb rubles for a piece of bread, ps for a piece of sausage and pc for a piece of cheese.

Polycarpus has r rubles and he is ready to shop on them. What maximum number of hamburgers can he cook? You can assume that Polycarpus cannot break or slice any of the pieces of bread, sausage or cheese. Besides, the shop has an unlimited number of pieces of each ingredient.

Input
The first line of the input contains a non-empty string that describes the recipe of “Le Hamburger de Polycarpus”. The length of the string doesn’t exceed 100, the string contains only letters ‘B’ (uppercase English B), ‘S’ (uppercase English S) and ‘C’ (uppercase English C).

The second line contains three integers nb, ns, nc (1 ≤ nb, ns, nc ≤ 100) — the number of the pieces of bread, sausage and cheese on Polycarpus’ kitchen. The third line contains three integers pb, ps, pc (1 ≤ pb, ps, pc ≤ 100) — the price of one piece of bread, sausage and cheese in the shop. Finally, the fourth line contains integer r (1 ≤ r ≤ 1012) — the number of rubles Polycarpus has.

Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

Output
Print the maximum number of hamburgers Polycarpus can make. If he can’t make any hamburger, print 0.

Examples
Input
BBBSSC
6 4 1
1 2 3
4
Output
2
Input
BBC
1 10 1
1 10 1
21
Output
7
Input
BSC
1 1 1
1 1 3
1000000000000
Output
200000000001

题意
这道题的题意大概就是说我们有一个字符串 可以分解为三种字符 每个字符我们都有一些初始值 可以用来构建字符串 然后说明了每个字符的价格和我们的总钱数 问最多能构造几个字符串

思路 
这道题刚开始时并没有想到用二分去解决 而是感觉像是DP类的题目 但它确实出现在了二分专题 最终用二分答案过了这一道题目 因为我们能在常数内完成Check函数的检测 而且我们汉堡包的数量可能特别大 对我们的logn复杂度来说非常友好 所以我们选择二分汉堡包的数量 Check函数检测汉堡包数量

AC代码

#include<iostream>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<cstdlib>
#include<utility>
#include<cstring>
using namespace std;

namespace{
    using ll = long long;
    ll Left,Right,ans;
    string str;
    ll tmpa,tmpb,tmpc;
    ll sum_B,sum_S,sum_C;
    ll sum_a,sum_b,sum_c;
    ll sum;
}

bool judge(ll mid){
    ll a = mid*sum_B;
    ll b = mid*sum_S;
    ll c = mid*sum_C;
    ll need_B = max(ll(0),a-tmpa);
    ll need_S = max(ll(0),b-tmpb);
    ll need_C = max(ll(0),c-tmpc);
    ll Spend = need_B * sum_a + need_S * sum_b + need_C * sum_c;
    if(Spend <= sum) return true;
    else return false;
}

int main(){
    cin >> str;
    for(auto x : str){
        if(x=='B') ++sum_B;
        else if(x=='S') ++sum_S;
        else if(x=='C') ++sum_C;
    }
    cin >> tmpa >> tmpb >> tmpc;
    cin >> sum_a >> sum_b >> sum_c;
    cin >> sum;
    Left = 0;Right = 1e13;
    while(Left<=Right){
        ll mid = Left + ((Right - Left) / 2);
        if(judge(mid)){
            ans = mid;
            Left = mid + 1;
        }else{
            Right = mid-1;
        }
    }
    cout << ans << endl;
    return 0;
}

原文链接: https://www.cnblogs.com/lizhaolong/p/16437380.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    Hamburgers CodeForces - 371C(二分答案)

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/395799

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月5日 下午1:43
下一篇 2023年4月5日 下午1:43

相关推荐