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 hasnb pieces of bread,ns pieces of sausage andnc pieces of cheese in the kitchen. Besides, the shop nearby has all three ingredients, the prices arepb rubles for a piece of bread,ps for a piece of sausage andpc for a piece of cheese.

Polycarpus hasr 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 EnglishB), 'S' (uppercase EnglishS) and 'C' (uppercase EnglishC).

The second line contains three integersnb,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 integerspb,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 integerr (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 thecin,cout streams or the%I64d specifier.
Output
Print the maximum number of hamburgers Polycarpus can make. If he can't make any hamburger, print0.
Examplesinput

BBBSSC6 4 11 2 34

output

2

input

BBC1 10 11 10 121

output

7

input

BSC1 1 11 1 31000000000000

output

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;
} //当然这并不是真正的贪心,再说吧。

原文链接: https://www.cnblogs.com/CLGYPYJ/p/7027998.html

欢迎关注

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

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

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

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

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

(0)
上一篇 2023年2月14日 上午8:52
下一篇 2023年2月14日 上午8:52

相关推荐