二分梳理


前言


这周学习了二分,借此篇博客来复习并梳理二分的内容。

一、二分适用的情况

大多数要用到二分的题一般都有以下特点:
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的影城管理系统,源码+数据库+论文答辩+毕业论文+视频演示 随着现在网络的快速发展,网上管理系统也逐渐快速发展起来,网上管理模式很快融入到了许多生活之中,随之就产生了“小徐影城管理系统”,这样就让小徐影城管理系统更加方便简单。 对于本小徐影城管理系统的设计来说,系统开发主要是采用java语言技术,在整个系统的设计中应用MySQL数据库来完成数据存储,具体根据小徐影城管理系统的现状来进行开发的,具体根据现实的需求来实现小徐影城管理系统网络化的管理,各类信息有序地进行存储,进入小徐影城管理系统页面之后,方可开始操作主控界面,主要功能包括管理员:首页、个人中心、用户管理、电影类型管理、放映厅管理、电影信息管理、购票统计管理、系统管理、订单管理,用户前台;首页、电影信息、电影资讯、个人中心、后台管理、在线客服等功能。 本论文主要讲述了小徐影城管理系统开发背景,该系统它主要是对需求分析和功能需求做了介绍,并且对系统做了详细的测试和总结。具体从业务流程、数据库设计和系统结构等多方面的问题。望能利用先进的计算机技术和网络技术来改变目前的小徐影城管理系统状况,提高管理效率。 关键词:小徐影城管理系统;Spring Boot框架,MySQL数据库
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值