ZCMU-1307: Hamburgers (二分)

铁废物我今天整这道题花了快2h····废物实锤···
这道题是个数据结构优化,哎,难~~~


题干

1307: Hamburgers

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 95 Solved: 34
[Submit][Status][Web Board]
Description

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 ≤ 106) — the number of rubles Polycarpus has.

Output

Print the maximum number of hamburgers Polycarpus can make. If he can’t make any hamburger, print 0.

Sample Input

BBBSSC
6 4 1
1 2 3
4
BBC
1 10 1
1 10 1
21

Sample Output

2
7

思想

这道题题意是这样的:
给你一串字符串(由B,S,C组成)作为一个产品的图纸,再给你nb,ns,nc个B,S,C,再告诉你商店里这三个原材料的价格和你目前的现金,求最多能制作多少个产品

看到这道题第一眼其实我觉得是模拟题的,然后试着写了一下发现卡住了。网上搜寻了一下发现是一道数据结构优化

AC代码

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define pre(i,a,b) for(int i=a;i>=b;--i)
#define m(x) memset(x,0,sizeof x)
const double PI = acos(-1);
const int maxn = 110;
typedef long long ll;
char H[maxn];
ll B,S,C;
ll nb,nc,ns;
ll pb,ps,pc;
ll sum1;
ll Start,End;

//寻找起始点 也就是最少可以制作的汉堡数
void get_start(){
    ll mi = 0x3f3f3f3f;
    if(B!=0&&(nb/B)<mi)mi = nb/B;
    if(S!=0&&(ns/S)<mi)mi = ns/S;
    if(C!=0&&(nc/C)<mi)mi = nc/C;
    Start = mi;
}

//最多能够做的汉堡数
void get_end(){
    ll sum;
    sum = sum1 + (pb*nb+ps*ns+pc*nc);
    End = sum / (B*pb+S*ps+C*pc);
}

//检查能否满足条件,可以就返回true
bool check(ll Mid)
{
    ll sum = 0;
    ll v1,v2,v3;
    v1 = (Mid*B - nb)*pb;
    v2 = (Mid*S - ns)*ps;
    v3 = (Mid*C - nc)*pc;
    if(v1 > 0)
        sum += v1;
    if(v2 > 0)
        sum += v2;
    if(v3 > 0)
        sum += v3;
    if(sum > sum1)
        return false;
    else
        return true;
}



//二分查找
void div(){
    ll mid = (Start+End)>>1;
    while(1)
    {
        mid = (Start+End)>>1;
        if(check(mid))Start = mid + 1;
        else End = mid - 1;
        if(check(mid)&&!check(mid+1))
            break;
    }
    printf("%lld\n",mid);
}

int main()
{
    while(scanf("%s",H)!=EOF)
       {
           scanf("%lld%lld%lld%lld%lld%lld%lld",&nb,&ns,&nc,&pb,&ps,&pc,&sum1);
           B = S = C = 0;
           int len = (int)strlen(H);
           //预处理过程
           for(int i=0; i<len; i++)
           {
               if(H[i] == 'B')
                   B++;
               else if(H[i] == 'S')
                   S++;
               else if(H[i] == 'C')
                   C++;
           }
           get_start();
           get_end();
           if(Start==0&&End==0)
           {
               printf("0\n");
               continue;
           }
           div();
      
       }
    return 0;
}


总结

二分不太会,反正这道题罚坐了很久

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值