对于二分有两种形式:
1.查找大于key的第一个值
2.查找小于key的第一个值
具体的实现形式:
http://blog.csdn.net/zouhust/article/details/11767703
这个blog讲的很详细
结合
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
BBBSSC 6 4 1 1 2 3 4
2
BBC 1 10 1 1 10 1 21
7
BSC 1 1 1 1 1 3 1000000000000
200000000001
对于这道题 先给出ac代码
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define INF 2000000000000
char str[105];
int main(){
int numb=0,nums=0,numc=0;
scanf("%s",str);
int len=strlen(str);
for(int i=0;i<len;i++){
if(str[i]=='B')
numb++;
if(str[i]=='S')
nums++;
if(str[i]=='C')
numc++;
}
int havb=0,havs=0,havc=0;
int pab,pas,pac;
ll mon=0;
scanf("%d%d%d",&havb,&havs,&havc);
scanf("%d%d%d",&pab,&pas,&pac);
scanf("%I64d",&mon);
ll l=0;
ll r=INF;
while(l<r){
ll mid=(l+r)/2;
ll bb=(mid*numb-havb)*pab;
ll ss=(mid*nums-havs)*pas;
ll cc=(mid*numc-havc)*pac;
if(bb<0)
bb=0;
if(ss<0)
ss=0;
if(cc<0)
cc=0;
if(bb+ss+cc<=mon)
l=mid+1;
else
r=mid;
}
printf("%I64d\n",r-1);
return 0;
}
核心的二分代码就是
<span style="color:#ffcccc;"> while(l<r){
if(bb+ss+cc<=mon)
l=mid+1;
else
r=mid;
}</span>
这一段了,其实就是在求大于mon的第一个值,然后求出来的答案就是r,但是r是符合大于mon的,但是题目的要求是小于mon的最大值嘛!所以答案就是r-1
这样总结了之后觉得以前的二分都是在瞎做QAQ