CodeForces 371C Hamburgers (二分)

A -
Hamburgers

Time Limit:1000MS    Memory Limit:262144KB    64bit IO Format:%I64d & %I64u

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?≤?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.

Sample Input

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

简单二分。

这是我们周赛时用vj扒的题,当时比赛时思路就是各种分情况,想想有点晕就没做,后来提醒说是二分,才自己想明白了,挺简单。二分求上界。

#include <stdio.h>
#include <string.h>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;
typedef __int64 ll;

const ll MAX=1000000000000+100;

ll need[4],pos[4],pri[4],money;	  //need做一个汉堡需要材料的量,pos拥有的,pri价格,money拥有的钱
char s[105];

//判断是否能够做出x个汉堡
bool C(ll x){
	ll i,sum=0;
	for(i=1;i<=3;i++)
		if(pos[i]<x*need[i])	//如果拥有的材料不够做x个,则计算需要用多少钱来买该材料
			sum+=pri[i]*(x*need[i]-pos[i]);
	return money>=sum;	//钱够返回true
}

ll upper_bound(){
	ll l,r,mid;
	l=0;r=MAX;
	while(l<r){
		mid=(l+r)/2+1;		//加1处理可以防止死循环,--b
		if(C(mid))
			l=mid;
		else
			r=mid-1;
	}
	return l;
}

int main()
{
	ll i,j,k;
	scanf("%s",s);
	scanf("%I64d%I64d%I64d",&pos[1],&pos[2],&pos[3]);
	scanf("%I64d%I64d%I64d%I64d",&pri[1],&pri[2],&pri[3],&money);
	memset(need,0,sizeof(need));
	for(i=0;s[i]!='\0';i++){
		if(s[i]=='B') need[1]++;
		if(s[i]=='S') need[2]++;
		if(s[i]=='C') need[3]++;
	}
	printf("%I64d\n",upper_bound());
	return 0;
}

时间: 2024-10-27 05:11:08

CodeForces 371C Hamburgers (二分)的相关文章

CodeForces 371C Hamburgers 二分

CodeForces 371C Hamburgers 二分 题意 给你一个做汉堡包的菜单,他们是由B S C,三种材料做成的,现在我们有一些材料和钱,我们想做最多的汉堡包,请问最多是多少? 解题思路 这里我们开始我们可能会想该怎么买,也就是买的策略是什么,其实我们可以不用去思考这个,理由如下: 假如我们知道最后的结果,我们是不是可以算出来我们要买的东西?答案是肯定的(在钱不浪费的情况下),再加上这个答案是线性单调的,也就是如果答案是m,那么小于m的也是一定可以做到的,这样我们就可以使用二分来枚举

Codeforces 371C Hamburgers

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 th

Codeforce 371C Hamburgers (二分答案)

题目链接 Hamburgers 二分答案,贪心判断即可. 1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define REP(i,n) for(int i(0); i < (n); ++i) 6 #define LL long long 7 8 char str[1010]; 9 LL len; 10 LL b, c, s, nb, nc, ns, pb, pc, ps; 11 LL money; 12 13 bool

CodeForces 424D: ...(二分)

题意:给出一个n*m的矩阵,内有一些数字.当你从一个方格走到另一个方格时,按这两个方格数字的大小,有(升,平,降)三种费用.你需要在矩阵中找到边长大于2的一个矩形,使得按这个矩形顺时针行走一圈的费用,与给定费用最接近.3<=n,m<=300. 思路:O(1)计算一个矩形的费用不是什么难事,因为考虑到有前缀性质(前缀性质:[l,r] = [0,r] - [0,l-1]),只要预处理好各行各个方向行走的费用,就容易计算. 直接枚举容易得到O(n^4)的算法.难以过.这时就应当想到优化.实际上,经过

Codeforces 460C prsent(二分答案)

//题意:给定N朵花的原先的高度,从左到右排列, //最多浇水m天,每天只能浇一次,每次使得连续的w朵花的高度增长1,问最后最矮的花的高度最高是多少. # include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int main() { __int64 n,m,w,l,r,i,m1,sum; __int64 a[200010],b[200010]; while(~

Codeforces 474B Worms (二分查找)

题目链接:Codeforces 474B Worms 题意:给出一串数字比如2 7 3 4 9. 表示第一堆编号是[1,2].第二堆编号是[3,9].第三堆编号是[10,12].第四堆编号是[13,16].第五堆编号是[17,25]. 预处理出每堆的上界二分查找答案. AC代码: </pre><pre name="code" class="cpp">#include<stdio.h> #include<map> #in

CodeForces 359D (数论+二分+ST算法)

题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=47319 题目大意:给定一个序列,要求确定一个子序列,①使得该子序列中所有值都能被其中一个值整除,②且子序列范围尽可能大(r-l尽可能大). 解题思路: 对于要求1,不难发现只有min(L,R)=gcd(L,R)时才行.其中gcd是L,R范围内的最大公约数,min是L,R范围内的最小值. 对于要求2,传统思路是r-l从大到小枚举,每次确定一个(L,R)范围,进行判

Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案

There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebo

CodeForces - 589A(二分+贪心)

题目链接:http://codeforces.com/problemset/problem/589/F 题目大意:一位美食家进入宴会厅,厨师为客人提供了n道菜.美食家知道时间表:每个菜肴都将供应. 对于第i道菜肴,他知道时间ai和bi的两个整数时刻(从宴会开始的几秒钟内) - ai为该菜端出来的时间,bi为该菜端走的时间(ai <BI).例如,如果ai = 10且bi = 11,那么第i个菜肴可在一秒钟内进食. 菜肴数量非常大,所以只要菜肴可以吃(即,在大厅里),它就无法用完. 美食家想要尝试每