前言
这周学习了二分,借此篇博客来复习并梳理二分的内容。
一、二分适用的情况
大多数要用到二分的题一般都有以下特点:
1.数据单调
2.需要在单调的数据中找到某一个符合条件的数
二、二分的一般流程
1.确定二分的对象
2.确定二分的边界
3.编写check函数或者说判断左右区间更新的条件
三、整数二分
1.需要注意的
出循环的时候,因为是l==r,所以两者皆可。
2.例题
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
分析:
在这道题中,可以很轻易地想到二分汉堡包的数量,当制作mid个汉堡包所需的钱大于已经有的钱时,便使r=mid。
代码如下:
#include<bits/stdc++.h>
using namespace std;
int b,s,c;
int pb,ps,pc;
long long mn,ans=0;
int B=0,S=0,C=0;
int main()
{
string h;
cin>>h;
cin>>b>>s>>c;//已经有的数量
cin>>pb>>ps>>pc;//每次qian
cin>>mn;//钱
int len=h.size();
for(int i=0;i<len;i++)
{
if(h[i]=='B')
{
B++;
}
else if(h[i]=='S')
{
S++;
}
else
{
C++;
}
}//每个汉堡需要的材料数
long long every=B*pb+C*pc+S*ps;//每个汉堡的钱
long long l=0,r=1e14;
long long mid=(r+l)/2;//制作汉堡个数
while (l<=r) {
long long mid = (l+r)/2;
long long cb=mid*B-b,cs=mid*S-s,cc=mid*C-c;
if(cb<0)cb=0;
if(cs<0)cs=0;
if(cc<0)cc=0;
long long nm=cb*pb+cs*ps+cc*pc;//需要的钱
if (nm<=mn)ans=mid;
if (nm>mn)r=mid-1;
else l=mid+1;
}
printf("%lld\n",ans);
}
需要注意的点:在计算制作mid个汉堡包剩余的各个材料时,若其小于0,则要让其等于0,否则便会啊吧啊吧。
四、浮点数二分
1.需要注意的地方
- 不同于整数二分,在浮点数二分中,若让r>l为跳出循环的条件,则可能因为循环次数过多而超时,所以,根据题目要求,一般会让r-l大于一个很小的数。(例如,题目要求最终结果与答案的偏差小于1e-6,则可以让r-l>1e-8)
例题
Now, here is a fuction:
F(x) = 6 * x7+8*x6+7x3+5*x2-yx (0 <= x <=100)
Can you find the minimum value when x is between 0 and 100.
Input
The first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has only one real numbers Y.(0 < Y <1e10)
Output
Just the minimum value (accurate up to 4 decimal places),when x is between 0 and 100.
Sample Input
2
100
200
Sample Output
-74.4291
-178.8534
题意:输入y的值,然后求出f(x)的最小值。
分析:通过对原方程的分析,可以得知, F(x) = 6 * x7+8*x6+7x3+5*x2-yx 并不是一个能判断单调性的函数,但通过对其求导,可以发现,它的导数具有单调性,因此,我们可以求出其导数的零点,从而得到F(x)的最小值。
#include<bits/stdc++.h>
using namespace std;
double f(double x)
{
return 42*pow(x,6)+48*pow(x,5)+21*pow(x,2)+10*x;
}
double F(double x,double y)
{
return 6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*pow(x,2)-y*x;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
double y;
scanf("%lf",&y);
double l=0.0,r=100.0;
double mid;
while(r-l>1e-9)
{
mid=(r+l)/2;
if(f(mid)>y)
{
r=mid;
}
else
{
l=mid;
}
}
printf("%0.4lf\n",F(mid,y));
}
return 0;
}