当前位置:网站首页>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;
}
边栏推荐
- Day37 JS note motion function 2021.10.11
- Cannot redeclare block range variables
- 6.A-B
- Random forest and poetry maker trained by AMR
- Thesis reading (59):keyword based diverse image retrieval with variable multiple instance graph
- Open3d manual clipping point cloud
- If you want to change to software testing, how can you package your resume as a test engineer with 1 year of work experience
- day23 js笔记 2021.09.14
- Chapter 2 do you remember the point, line and surface (2)
- day32 js笔记 事件(上)2021.09.27
猜你喜欢

Remote login sshd service

《运营之光3.0》全新上市——跨越时代,自我颠覆的诚意之作!

Graphics view framework for QT learning (to realize startup animation)

Day33 JS note event (Part 2) September 28, 2021

功能真花哨,价格真便宜!长安全新SUV真实力到底怎样?

Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system

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

Zero foundation self-study SQL course | if function

如临现场的视觉感染力,NBA决赛直播还能这样看?

Day39 prototype chain and page Fireworks Effect 2021.10.13
随机推荐
day33 js笔记 事件(下)2021.09.28
无法重新声明块范围变量
赛尔号抽奖模拟求期望
Is it feasible to be a programmer at the age of 26?
【sciter】: sciter-fs模块扫描文件API的使用及其注意细节
Come on, yuanuniverse. Sure enough, the heat won't pass for a while
IO stream of file and Base64
[no title] the virtual machine vmnet0 cannot be found and an error is reported: there is no un bridged host network adapter
携手Cigent:群联为SSD主控固件引入高级网络安全防护特性
2022 开源软件安全状况报告:超41%的企业对开源安全没有足够的信心
太阳能无线LED显示屏的特点
Day34 JS notes regular expression 2021.09.29
培训通知|2022年境外中资企业机构及人员疫情防控和安全防范专题培训通知
Zero foundation self-study SQL course | if function
6.A-B
day31 js笔记 DOM下 2021.09.26
ProCAST finite element casting process simulation software
MapReduce项目案例1
js中的数组方法 2021.09.18
Interview skills for interview steps