当前位置:网站首页>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;
}
边栏推荐
- Necessary for beginners PR 2021 quick start tutorial, PR green screen matting operation method
- Contract quantification system development (construction explanation) - contract quantification system development (source code analysis and ready-made cases)
- Which programming language will attract excellent talents?
- Makefile introduction
- 【sciter】: sciter-fs模块扫描文件API的使用及其注意细节
- MySQL cannot query the maximum value using the max function
- Day39 prototype chain and page Fireworks Effect 2021.10.13
- Day30 JS notes BOM and DOM 2021.09.24
- TiDB v6.0.0 (DMR) :缓存表初试丨TiDB Book Rush
- day30 js笔记 BOM和DOM 2021.09.24
猜你喜欢

String & heap & method area

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

Oracle date format exception: invalid number

Array method in JS 2021.09.18

Packaging and publishing application of jetpack compose desktop version

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

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

Share the easy-to-use fastadmin open source system - practical part

人人都可以参与开源!龙蜥社区最不容错过的开发者活动来了

The default point of this in JS and how to modify it to 2021.11.09
随机推荐
FTP protocol for Wireshark packet capture analysis
Recommended practice sharing of Zhilian recruitment based on Nebula graph
The development and principle of the metacosmic system
Adding a new user in MySQL 5.7
Allez, Meta - Cosme, comme prévu, cette chaleur ne durera pas longtemps.
网页提示此站点不安全解决方案
100 important knowledge points that SQL must master: retrieving data
recent developments
5. Sum of N numbers
day39 原型链及页面烟花效果 2021.10.13
4. maximum continuity factor
《运营之光3.0》全新上市——跨越时代,自我颠覆的诚意之作!
ProCAST finite element casting process simulation software
[no title] the virtual machine vmnet0 cannot be found and an error is reported: there is no un bridged host network adapter
day36 js笔记 ECMA6语法 2021.10.09
MySQL cannot query the maximum value using the max function
Solutions to connection failures and errors when accessing mysql8 using the SSM project
2018 joint examination of nine provinces & Merging of line segment trees
Mutual conversion between mytipartfile and file
Which broker is safer and more convenient to open an account for Oriental Fortune mobile stock?