C. 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.
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.
Print the maximum number of hamburgers Polycarpus can make. If he can't make any hamburger, print 0.
BBBSSC
6 4 1
1 2 3
4
2
BBC
1 10 1
1 10 1
21
7
BSC
1 1 1
1 1 3
1000000000000
200000000001
http://codeforces.com/problemset/problem/371/C
#include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #include<queue> #include<math.h> using namespace std; int B,S,C;//一个汉堡需要的 int nb,nc,ns;//已有的 int pb,pc,ps;//单价 long long money; long long l,r,mid; bool check(long long mid) { long long need=0; if(mid*B>nb) need+= (mid*B-nb)*pb; if(mid*S>ns) need += (mid*S-ns)*ps; if(mid*C>nc) need += (mid*C-nc)*pc; return need<=money; } int main() { char a[110];int len; cin>>a+1;len=strlen(a+1); cin>>nb>>ns>>nc; cin>>pb>>ps>>pc; cin>>money; for(int i=1;i<=len;i++) { if(a[i]=='B') B++;else if (a[i]=='C') C++;else if(a[i]=='S') S++; } l=0;r=money+max(max(nb,nc),ns);//这里要考虑最大值,原来的,加钱数。
二分时,一定要注意左右边界!!!!! while(l<=r)//二分答案,能做出的汉堡数 { mid=(l+r)>>1; if(check(mid)) l=mid+1; else r=mid-1; } cout<<r; return 0; }
#include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #include<queue> #include<math.h> using namespace std; int B,S,C;//一个汉堡需要的 int nb,nc,ns;//已有的 int pb,pc,ps;//单价 long long money; long long ans; long long need=0; bool check(int mid) { need=0; if(mid*B>nb&&B) need+= (mid*B-nb)*pb; if(mid*S>ns&&S) need += (mid*S-ns)*ps; if(mid*C>nc&&C) need += (mid*C-nc)*pc; return need<=money; } int main() { char a[110];int len; cin>>a+1;len=strlen(a+1); cin>>nb>>ns>>nc; cin>>pb>>ps>>pc; cin>>money; for(int i=1;i<=len;i++) { if(a[i]=='B') B++;else if (a[i]=='C') C++;else if(a[i]=='S') S++; } while(check(10000)) { nb=nc=ns=0; ans+=10000; money-=need; } while(check(1000)) { nb=nc=ns=0; ans+=1000; money-=need; } while(check(100)) { nb=nc=ns=0; ans+=100; money-=need; } while(check(1)) { if(B>=nb) nb=0;else nb-=B; if(S>=ns) ns=0;else ns-=S; if(C>=nc) nc=0;else nc-=C;//这个处理就花费了我大部分时间,本来是写在check里的但是,对剩余材料的处理,必须是check==1时才能进行。所以要写在while里 ans+=1; money-=need; } cout<<ans; return 0; } //当然这并不是真正的贪心,再说吧。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App