当前位置:网站首页>Bisection (integer bisection and floating point bisection)
Bisection (integer bisection and floating point bisection)
2022-06-28 11:57:00 【Q_ ming_ code】
Dichotomy
Problems that dichotomy can solve
It is used to find a single point problem that satisfies the meaning of the problem , Some personality quality divides the interval into satisfactions and unsatisactions
Two halves of , The dichotomy can find the two boundaries of this property ( That is, the boundary that does not satisfy the property and the boundary that satisfies the property
The border ), But it is very important to pay attention to the boundary problem
The essence of dichotomy
The essence of dichotomy is to replace search with a decision problem , Gradually narrow the range
Lock the answer , The essence of two parts is not monotonicity .
The condition of dichotomy
Be sure to look in an ordered sequence of numbers .
The idea of dichotomy
Set a section [l,r], Some property will be interval [l,r] In two parts , The first part does not satisfy this property , The latter half satisfies this property , The dividing point of the first part is point (1) Interval is [l,(1)], The dividing point of the latter part is point (2) Interval is [(2),r].
1. Find the interval [l,r] In the middle mid
2. Check mid Whether this property is satisfied , Determine whether it is satisfied or not , according to mid Update the interval to narrow the range .
To get a dichotomous question, we must first think about which property can be divided into good intervals , determine mid How to narrow the interval after .
Split template ( Integer dichotomy )
There are two bisection templates according to different intervals
1. When the interval [l,r] Divide into [l,mid] and [mid+1,r] when ,
update operation
r=mid( The answer range is 【l,mid】)
or l=mid+1( Answer interval 【mid+1,r】)
Calculation mid There is no need to add 1.( General point finding (2))
int bsearch_1(int l,int r)
{
while(l<r)
{
int mid=l+r>>1; // Round down
if(check(mid)) r=mid;
else l=mid+1;
}
return l;
}
2. When the interval [l,r] Divide into [l,mid-1] and [mid,r] when ,
update operation
r=mid-1( The answer range is 【mid,r】)
or l=mid( The answer range is 【l,mid-1】)
Calculation mid Need to add 1.( General point finding (1))
int bsearch_2(int l,int r)
{
while(l<r)
{
int mid=l+r+1>>1; // Round up
if(check(mid)) l=mid;
else r=mid-1;
}
return 1;
}
Floating point binary
Compared with integer bisection, floating-point bisection has no boundary , You can also use the template of integer bisection
Here are some examples to explain :
eg: seek x The square root of
#include<iostream>
using namespace std;
int main()
{
double x;
cin>>x;
double l=0,r=x;
while(r-l>1e-8)
{
double mid=(l+r)/2;
if(mid*mid>=x)
r=mid;
else
l=mid;
}
printf("%lf\n",l);
return 0;
}
边栏推荐
- 2022 开源软件安全状况报告:超41%的企业对开源安全没有足够的信心
- day32 js笔记 事件(上)2021.09.27
- ProCAST finite element casting process simulation software
- 3. seat number
- day33 js笔记 事件(下)2021.09.28
- Day39 prototype chain and page fireworks effect 2021.10.13
- 工作组环境下的内网渗透:一些基础打法
- day28 严格模式、字符串 js 2021.09.22
- Day32 JS note event (Part 1) September 27, 2021
- How to deploy the software testing environment?
猜你喜欢

day39 原型鏈及頁面烟花效果 2021.10.13

Day31 JS notes DOM 2021.09.26

Recommended practice sharing of Zhilian recruitment based on Nebula graph

Day29 JS notes 2021.09.23

Thesis reading (59):keyword based diverse image retrieval with variable multiple instance graph

Array method in JS 2021.09.18

Class pattern and syntax in JS 2021.11.10

day30 js笔记 BOM和DOM 2021.09.24

Day39 prototype chain and page Fireworks Effect 2021.10.13

Oracle date format exception: invalid number
随机推荐
Necessary for beginners PR 2021 quick start tutorial, PR green screen matting operation method
Contract quantitative trading system development | contract quantitative app development (ready-made cases)
Random forest and poetry maker trained by AMR
Share the easy-to-use fastadmin open source system - practical part
day36 js笔记 ECMA6语法 2021.10.09
Characteristics of solar wireless LED display
QML control type: tabbar
2022 open source software security status report: over 41% of enterprises do not have enough confidence in open source security
使用ssm项目对Mysql8进行访问的时候,出现连接失败和一些错误的解决办法
Lihongyi, machine learning 7 Conclusion
Splicing strings in the string collection_ Stream based
day39 原型链及页面烟花效果 2021.10.13
[sciter]:sciter如何使用i18实现桌面应用多语言切换及其利弊
基于验证码识别的机器学习项目captcha_trainer操作实践
NFT card chain game system development DAPP construction technical details
培训通知|2022年境外中资企业机构及人员疫情防控和安全防范专题培训通知
6. calculation index
携手Cigent:群联为SSD主控固件引入高级网络安全防护特性
day28 严格模式、字符串 js 2021.09.22
day31 js笔记 DOM下 2021.09.26