[Codeforces]Hamburgers

原题链接

Codeforces真是太棒了
全英文请自行翻译

二分
注意边界条件
再次因为这个错了

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<queue>
#include<vector>
#include<climits>
#include<string>
#include<cstdlib>
#include<map>
#include<ctime>
#define MAX 1000000007
#define LL long long
using namespace std;

char h[105];
LL len,i,y[4],nb,ns,nc,pb,ps,pc,t,t2;
LL rub,l,r;

LL check(LL x)
{
    LL mon,ned;

    mon=0;

    ned=x*y[1];
    if(ned>nb) mon+=(ned-nb)*pb;

    ned=x*y[2];
    if(ned>ns) mon+=(ned-ns)*ps;

    ned=x*y[3];
    if(ned>nc) mon+=(ned-nc)*pc;

    return mon<=rub;    
}

int main()
{
    scanf("%s",h);

    len=strlen(h);

    for(i=0;i<len;i++)
    {
        if(h[i]=='B') y[1]++;
        if(h[i]=='S') y[2]++;
        if(h[i]=='C') y[3]++; 
    }

    scanf("%I64d%I64d%I64d",&nb,&ns,&nc);
    scanf("%I64d%I64d%I64d",&pb,&ps,&pc);
    scanf("%I64d",&rub);

    r=1e12+100;

    while(l<=r)
    {
        LL mid=(l+r)/2;

        t=check(mid);

        if(t) l=mid+1;
        else r=mid-1;
    }

    t=check(r);

    if(t) printf("%I64d",r);
    else printf("0");   

    return 0;
} 

贪心

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<queue>
#include<vector>
#include<climits>
#include<string>
#include<cstdlib>
#include<map>
#include<ctime>
#define MAX 1000000007
#define LL long long
using namespace std;

char h[105];
LL i,len,y[4],n[4],p[4],cb,ans,rub;

int main()
{
    scanf("%s",h);

    len=strlen(h);

    for(i=0;i<len;i++)
    {
        if(h[i]=='B') y[1]++;
        if(h[i]=='S') y[2]++;
        if(h[i]=='C') y[3]++; 
    }

    scanf("%I64d%I64d%I64d",&n[1],&n[2],&n[3]);
    scanf("%I64d%I64d%I64d",&p[1],&p[2],&p[3]);
    scanf("%I64d",&rub);

    cb=p[1]*y[1]+p[2]*y[2]+p[3]*y[3];

    while((n[1]||n[2]||n[3])&&rub)
    {

        if(!y[1]) n[1]=0;
        if(!y[2]) n[2]=0;       
        if(!y[3]) n[3]=0;

        if(y[1])
        {
            if(n[1]>y[1]) n[1]-=y[1];
            else 
            {
                rub-=(y[1]-n[1])*p[1];
                n[1]=0; 
                if(rub<0) break;            
            }           
        }

        if(y[2])
        {
            if(n[2]>y[2]) n[2]-=y[2];
            else 
            {
                rub-=(y[2]-n[2])*p[2];
                n[2]=0; 
                if(rub<0) break;        
            }

        }

        if(y[3])
        {
            if(n[3]>y[3]) n[3]-=y[3];
            else 
            {
                rub-=(y[3]-n[3])*p[3];
                n[3]=0;
                if(rub<0) break;        
            }           
        }

        ans++;          
    }

    if(rub>0) ans+=rub/cb;

    printf("%I64d",ans);

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值