当前位置:网站首页>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;
}
边栏推荐
- Day30 JS notes BOM and DOM 2021.09.24
- 使用ssm项目对Mysql8进行访问的时候,出现连接失败和一些错误的解决办法
- 5. Sum of N numbers
- Day36 JS notes ecma6 syntax 2021.10.09
- What method is required for word, PDF and txt files to realize full-text content retrieval?
- MySQL cannot query the maximum value using the max function
- Training notice | special training notice on epidemic prevention and security prevention for overseas Chinese funded enterprises, institutions and personnel in 2022
- [no title] the virtual machine vmnet0 cannot be found and an error is reported: there is no un bridged host network adapter
- Get current system date
- Timestamp and date conversion "suggested collection"
猜你喜欢
The default point of this in JS and how to modify it to 2021.11.09
Using soapUI to obtain freemaker's FTL file template
Web3安全连载(3) | 深入揭秘NFT钓鱼流程及防范技巧
How to deploy the software testing environment?
Day36 JS notes ecma6 syntax 2021.10.09
Graphics view framework for QT learning (to realize startup animation)
day30 js笔记 BOM和DOM 2021.09.24
If you want to change to software testing, how can you package your resume as a test engineer with 1 year of work experience
QML control type: tabbar
Everyone can participate in open source! Here comes the most important developer activity in dragon lizard community
随机推荐
Fruit FL studio/cubase/studio one music host software comparison
[no title] the virtual machine vmnet0 cannot be found and an error is reported: there is no un bridged host network adapter
Open3d manual clipping point cloud
來吧元宇宙,果然這熱度一時半會兒過不去了
基于验证码识别的机器学习项目captcha_trainer操作实践
使用ssm项目对Mysql8进行访问的时候,出现连接失败和一些错误的解决办法
The development and principle of the metacosmic system
Intranet penetration in the working group environment: some basic methods
Day33 JS note event (Part 2) September 28, 2021
When an entity is converted to JSON, the field with null value is lost
Contract quantitative trading system development | contract quantitative app development (ready-made cases)
水果FL Studio/Cubase/Studio one音乐宿主软件对比
The default point of this in JS and how to modify it to 2021.11.09
Simulation of the Saier lottery to seek expectation
Connectionreseterror: [winerror 10054] the remote host forced an existing connection to be closed
如临现场的视觉感染力,NBA决赛直播还能这样看?
Timestamp and date conversion "suggested collection"
Use logrotate to automatically cut the website logs of the pagoda
面试步骤的面试技巧
4. maximum continuity factor