CodeForces - 371C Hamburgers (二分)

CodeForces - 371C 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.

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.

Examples
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

題目大意:

要做一個漢堡 需要 BSC三種原料
第一行 給你一個字符串,字符串中有多少B 就是做一個漢堡需要多少B 有多少C就是需要多少C,有多少S就是需要多少S
第二行 告訴你 你現在有多少 B S C
第三行 告訴你 買一個 B S C分別花多少錢
第四行 告訴你現在有多少錢

問 : 最多能做多少個漢堡

解題思路:

二分我們能做的漢堡個數,然後檢測是不是可以做出這麼些漢堡。
簡單地二分。

AC代碼:

#include <iostream>
#include <cstdio>
#include <string>
#define ll long long
using namespace std;
string s;
ll need_1,need_2,need_3;
ll hava_1,hava_2,hava_3;
ll val_1,val_2,val_3;
ll hava_money;
bool judge(ll mid){
	ll n1 = mid*need_1;
	ll n2 = mid*need_2;
	ll n3 = mid*need_3;
	ll cha1 = n1 - hava_1;
	if(cha1<0) cha1 = 0;
	ll cha2 = n2 - hava_2;
	if(cha2<0) cha2 = 0;
	ll cha3 = n3 - hava_3;
	if(cha3<0) cha3 = 0;
	ll cost = 0;
	cost = cha1*val_1+cha2*val_2+cha3*val_3;
	if(cost<=hava_money) return true;
	else return false;
}
int main(){
	cin>>s;
	int len = s.length();
	cin>>hava_1>>hava_2>>hava_3;
	cin>>val_1>>val_2>>val_3;
	cin>>hava_money;
	for(int i=0;i<len;i++){
		if(s[i]=='B'){
			need_1++;
		}else if(s[i]=='S'){
			need_2++;
		}else if(s[i]=='C'){
			need_3++;
		}
	}
	ll left = 0;
	ll right = 1e15;
	ll ans = 0;
	while(left<=right){
		ll mid = (left+right)/2;
		if(judge(mid)){
			ans = mid;
			left = mid+1;
		}else{
			right = mid-1;
		}
	}
	printf("%lld\n",ans);
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章