当前位置:网站首页>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;
}
边栏推荐
- Industry analysis - quick intercom, building intercom
- JS foundation 8
- day33 js笔记 事件(下)2021.09.28
- Oracle date format exception: invalid number
- 行业分析| 快对讲,楼宇对讲
- Batch will png . bmp . JPEG format pictures are converted to Jpg format picture
- day28 严格模式、字符串 js 2021.09.22
- Day31 JS notes DOM 2021.09.26
- Using soapUI to obtain freemaker's FTL file template
- day37 js笔记 运动函数 2021.10.11
猜你喜欢

Day31 JS notes DOM 2021.09.26

Jetpack Compose Desktop 桌面版本的打包和发布应用

Django -- MySQL database reflects the mapping data model to models

js中的class类模式及语法 2021.11.10

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

For example, the visual appeal of the live broadcast of NBA Finals can be seen like this?

Everyone can participate in open source! Here comes the most important developer activity in dragon lizard community

New listing of operation light 3.0 - a sincere work of self subversion across the times!

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

Share the easy-to-use fastadmin open source system - practical part
随机推荐
Which programming language will attract excellent talents?
Solutions to connection failures and errors when accessing mysql8 using the SSM project
分析list中有无重复数据且重复了几次
Timestamp and date conversion "suggested collection"
Class pattern and syntax in JS 2021.11.10
董宇辉,新东方以及凤凰卫视
6. calculation index
【sciter】: sciter-fs模块扫描文件API的使用及其注意细节
Redis6 1: what problems can be solved by the introduction of NoSQL and redis?
Array method in JS 2021.09.18
IO stream of file and Base64
2. single digit statistics
day34 js笔记 正则表达式 2021.09.29
【无标题】虚拟机vmnet0找不到且报错:没有未桥接的主机网络适配器
來吧元宇宙,果然這熱度一時半會兒過不去了
It is safer for individuals to choose which securities company to open an account for buying floor funds
day39 原型链及页面烟花效果 2021.10.13
[no title] the virtual machine vmnet0 cannot be found and an error is reported: there is no un bridged host network adapter
买股票在中金证券经理的开户二维码上开户安全吗?求大神赐教
Simulation of the Saier lottery to seek expectation