二分法:CF371C-Hamburgers(二分法+字符串的处理)
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 %I64dspecifier.
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
解题心得:
和二分答案差不多,就不多说了,只是多了一个字符串的处理
详情看点击打开链接
#include<bits/stdc++.h>
using namespace std;
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; 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 get_Start()
{
ll Min = 0x7f7f7f7f;
if(B != 0 && nb / B < Min)
Min = nb / B;
if(S != 0 && ns / S < Min)
Min = ns / S;
if(C != 0 && nc / C < Min)
Min = nc / C;
Start = Min;
}
void get_End()
{
ll sum = 0;
sum = sum + sum1 + nb*pb + ns*ps + nc*pc;
End = sum / (B*pb + S*ps + C*pc);
} void div()
{
ll Mid;
while(1)
{
Mid = (Start + End) / 2;
if(!check(Mid))
End = Mid - 1;
else
Start = 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 = 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();
}
}
二分法:CF371C-Hamburgers(二分法+字符串的处理)的更多相关文章
- I - Beautiful People ZOJ - 2319 (二分法)
The most prestigious sports club in one city has exactly N members. Each of its members is strong an ...
- Python基础之递归函数与二分法
一.递归函数 定义: 在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. 我们来举个例子吧,比如:有个人问“egon”年龄,他说比“小大”大5岁,“小大”又说比“小保 ...
- python 内置函数(二) 进阶函数 递归内容及二分法查找 知识点
1,lambda: 匿名函数 2.sorgted() 排序函数 3,filter() 过滤函数 筛选 4,map() 映射函数 5.递归 6.二分法 一. 匿名函数: lambda lamb ...
- Java实现二分法排序
二分法:(二分法不是只能做数组,这里的数组只是为了举例) 在给出的有序排列的数组中,把目标值和数组中间值进行比较,如果相等,则返回中间值下标,如果目标值小于中间值,就从数组的前半段再次执行二分法查找, ...
- iOS 排序算法总结、二分法查找
1.插入排序 在要排序的一组数中,假设前面(n-1) [n>=2] 个数已经是排好顺序的,现在要把第n个数插到前面的有序数中,使得这n个数也是排好顺序的.如此反复循环,直到全部排好顺序. 直接插 ...
- 33,Leetcode 搜索旋转排序数组-C++ 递归二分法
题目描述 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 搜索一个给定的目标值,如果数组中存在这 ...
- Leetcode 69. Sqrt(x)及其扩展(有/无精度、二分法、牛顿法)详解
Leetcode 69. Sqrt(x) Easy https://leetcode.com/problems/sqrtx/ Implement int sqrt(int x). Compute an ...
- LeetCode刷题模板(1):《我要打10个》之二分法
Author : 叨陪鲤 Email : vip_13031075266@163.com Date : 2021.01.23 Copyright : 未 ...
- Python基础之函数:4、二分法、三元表达式、生成/推导式、匿名函数、内置函数
目录 一.算法简介之二分法 1.什么是算法 2.算法的应用场景 3.二分法 二.三元表达式 1.简介及用法 三.各种生成式 1.列表生成式 2.字典生成式 3.集合生成式 四.匿名函数 五.常见内置函 ...
随机推荐
- 将MySQL转化为mysqli
<?php/** * Created by PhpStorm. * User: 大神 * Date: 2017/7/24 * Time: 11:29 */ header('content-typ ...
- arcgis 定位图斑,并且高亮显示
///图斑定位 function TabQuery(instance_id, layer_name) { require(["esri/map", "esri/geome ...
- java反射-使用反射获取类的所有信息
在OOP(面向对象)语言中,最重要的一个概念就是:万事万物皆对象. 在java中,类也是一个对象,是java.lang.Class的实例对象,官网称该对象为类的类类型. Class 类的实例表示正在运 ...
- Fiddler 抓包工具总结(转)
Fiddler 抓包工具总结 阅读目录 1. Fiddler 抓包简介 1). 字段说明 2). Statistics 请求的性能数据分析 3). Inspectors 查看数据内容 4). Au ...
- smarty基本用法
简介: 1.smarty语法:它是php的一种模板引擎 它的设计特点是:业务逻辑与显示逻辑分离 Smarty的标签都是使用定界符{ }括起来注释:{* 我是Smarty的注释内容 *} <u ...
- 【干货】JavaScript DOM编程艺术学习笔记7-9
七.动态创建标记 在文档中不写占位图片和文字代码,在能调用js的情况下动态创建,文档支持性更好. 在原来的addLoadEvent prepareGallery showPic的基础上增加函数prep ...
- Flexbox与Grid属性比较
网格容器(container)属性 网格项目(item)属性 Flex容器(container)属性 Flex项目(item)属性
- LeetCode Remove Duplicates from Sorted List 删除有序链表中的重复结点
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- HDU 3697 Selecting courses 选课(贪心)
题意: 一个学生要选课,给出一系列课程的可选时间(按分钟计),在同一时刻只能选一门课程(精确的),每隔5分钟才能选一次课,也就是说,从你第一次开始选课起,每过5分钟,要么选课,要么不选,不能隔6分钟再 ...
- 初学树套树:线段树套Treap
前言 树套树是一个十分神奇的算法,种类也有很多:像什么树状数组套主席树.树状数组套值域线段树.\(zkw\)线段树套\(vector\)等等. 不过,像我这么弱,当然只会最经典的 线段树套\(Trea ...