博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【题解】Hamburgers CodeForces - 371C ⭐⭐⭐ 【二分+贪心】
阅读量:1888 次
发布时间:2019-04-26

本文共 3252 字,大约阅读时间需要 10 分钟。

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

Hint

题意:

做一个汉堡需要BSC材料数如字符串所示, 给出当前拥有的材料数量, 每个材料的价格, 以及有多少钱, 问最多能做几个汉堡

题解:

二分汉堡数量, 贪心的来凑

经验小结:

#include
using namespace std;#define ms(x, n) memset(x,n,sizeof(x));typedef long long LL;const int inf = 1 << 30;const int maxn = 110;LL need[3], cnt[3], price[3], money;bool check(LL t){
LL sum = 0; for(int i = 0; i < 3; ++i) sum += max(t*need[i]-cnt[i], 0LL) * price[i]; return money >= sum;}int main() {
string s; cin >> s >> cnt[0] >> cnt[1] >> cnt[2] >> price[0] >> price[1] >> price[2] >> money; for(int i = 0; i < s.length(); ++i){
if(s[i] == 'B') ++need[0]; if(s[i] == 'S') ++need[1]; if(s[i] == 'C') ++need[2]; } LL mmin = inf, ans = 0; for(int i = 0; i < 3; ++i){
if(need[i] == 0) continue; mmin = min(mmin, cnt[i]/need[i]); } ans += mmin; for(int i = 0; i < 3; ++i) cnt[i] -= need[i]*mmin; LL l = 0, r = money+cnt[0]+cnt[1]+cnt[2]; while(l < r){
LL mid = (l+r+1)/2; if(check(mid)) l = mid; else r = mid-1; } cout << ans+l << endl; return 0;}

转载地址:http://uvbdf.baihongyu.com/

你可能感兴趣的文章
效率提升法则:高效人士不会去做的4件事
查看>>
8.PostgreSQL约束
查看>>
【技术分享】使用AES加密技术保障数据安全
查看>>
【应用实例】布线多?成本高?不可靠?泽耀方案没烦恼!
查看>>
数据可视化工具:Matplotlib绘图
查看>>
用Python写个超级小恐龙跑酷游戏,上班摸鱼我能玩一天
查看>>
闺蜜看我用Python画了一幅樱花图,吵着要我给他介绍程序员小哥哥
查看>>
【Python爬虫实战】知乎热榜数据采集,上班工作摸鱼两不误,知乎热门信息一网打尽
查看>>
自从我学会了数据挖掘Matplotlib、Numpy、Pandas、Ta-Lib等一系列库,我把领导开除了
查看>>
Python抓取哔哩哔哩up主信息:只要爬虫学的好,牢饭吃的早
查看>>
有个码龄5年的程序员跟我说:“他连wifi从来不用密码”
查看>>
领导让我整理上个季度的销售额,幸好我会Python数据分析,你猜我几点下班
查看>>
【Python爬虫实战】为何如此痴迷Python?还不是因为爱看小姐姐图
查看>>
零基础自学Python,你也可以实现经济独立!
查看>>
ElasticSearch与Mysql对比(ElasticSearch常用方法大全,持续更新)
查看>>
数字化转型的主干道上,华为云以“三大关键”成企业智能化推手
查看>>
数字化为何不走“捷”“径”?
查看>>
和总裁、专家交朋友,华为云助推政企智能化升级又做到前面去了
查看>>
BCOP章鱼船长,6月22日晚上8点上线薄饼
查看>>
为战疫助力,半导体功不可没
查看>>