当前位置:   article > 正文

Hamburgers CodeForces - 371C(二分答案)_polycarpus loves hamburgers测试点

polycarpus loves hamburgers测试点

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/494394
推荐阅读
相关标签
  

闽ICP备14008679号