当前位置:网站首页>Dichotomy (integer dichotomy, real dichotomy)
Dichotomy (integer dichotomy, real dichotomy)
2022-07-06 17:59:00 【Stellaris_ L】
Two 、 Two points
Search method in monotone sequence or monotone function .( Termination conditions :l=r)
Integer field dichotomy
analysis
- Pay attention to the end boundary 、 Left and right intervals .
(1) Narrow the scope of :r=mid,l=mid+1, Median value :mid=(l+r)>>1.
(2) Narrow the scope of :l=mid,r=mid+r, Median value :mid=(l+r+1)>>1.
The first kind won't get r r r This value , The second kind won't get l l l. It can be used to deal with the situation of no solution . The first two partitions are from [1,n] Expand to [1,n+1] and [0,n], If the last two points stop at this out of bounds subscript , There is no solution. .
huangzh
- Right shift operation rounding down , Integer division rounds zero .
Templates
Example
789. The range of numbers - AcWing Question bank
#include<iostream>
using namespace std;
const int N=1e5+10;
int a[N];
int n,q,k;
int main(){
cin>>n>>q;
for(int i=0;i<n;i++)cin>>a[i];
while(q--){
int k;
cin>>k;
int l=0,r=n-1;
while(l<r){
int mid=l+r>>1;
if(a[mid]>=k)r=mid;
else l=mid+1;
}
if(a[l]!=k)cout<<"-1 -1"<<endl;
else {
cout<<l<<" ";
l=0,r=n-1;
while(l<r){
int mid=l+r+1>>1;
if(a[mid]<=k)l=mid;
else r=mid-1;
}
cout<<l<<endl;
}
}
return 0;
}
Real number field dichotomy
analysis
- Pay attention to accuracy
Using the bisection method with fixed number of cycles to solve the problem of accuracy .( Precision than eps high )
Templates
for(int i=0;i<100;i++){
// seek mid
double mid=(l+r)/2;
if(calc(mid))r=mid; else l=mid;
}
Example
790. The third root of a number - AcWing Question bank
- eps Accuracy requirements for
#include <iostream>
using namespace std;
int main(){
double n;
cin>>n;
double l=-10000,r=10000;
while(r-l>1e-8){
double mid=(l+r)/2;
if(mid*mid*mid>=n)r=mid;
else l=mid;
}
printf("%.6lf",l);
return 0;
}
边栏推荐
- The difference between parallelism and concurrency
- 78 岁华科教授逐梦 40 载,国产数据库达梦冲刺 IPO
- node の SQLite
- FlutterWeb瀏覽器刷新後無法回退的解决方案
- FMT开源自驾仪 | FMT中间件:一种高实时的分布式日志模块Mlog
- Pytest learning ----- pytest operation mode and pre post packaging of interface automation testing
- 编译原理——预测表C语言实现
- Unity particle special effects series - treasure chest of shining stars
- OliveTin能在网页上安全运行shell命令(上)
- [introduction to MySQL] the first sentence · first time in the "database" Mainland
猜你喜欢
8位MCU跑RTOS有没有意义?
Is it meaningful for 8-bit MCU to run RTOS?
Grafana 9 is officially released, which is easier to use and more cool!
分布式不来点网关都说不过去
Pourquoi Li shufu a - t - il construit son téléphone portable?
重磅!蚂蚁开源可信隐私计算框架“隐语”,主流技术灵活组装、开发者友好分层设计...
Reppoints: advanced order of deformable convolution
Scratch epidemic isolation and nucleic acid detection Analog Electronics Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022
C # nanoframework lighting and key esp32
历史上的今天:Google 之母出生;同一天诞生的两位图灵奖先驱
随机推荐
Getting started with pytest ----- test case rules
FlutterWeb瀏覽器刷新後無法回退的解决方案
scratch疫情隔离和核酸检测模拟 电子学会图形化编程scratch等级考试三级真题和答案解析2022年6月
Fleet tutorial 13 basic introduction to listview's most commonly used scroll controls (tutorial includes source code)
Stealing others' vulnerability reports and selling them into sidelines, and the vulnerability reward platform gives rise to "insiders"
Establishment of graphical monitoring grafana
FMT开源自驾仪 | FMT中间件:一种高实时的分布式日志模块Mlog
SAP UI5 框架的 manifest.json
Guidelines for preparing for the 2022 soft exam information security engineer exam
Summary of Android interview questions of Dachang in 2022 (I) (including answers)
Summary of Android interview questions of Dachang in 2022 (II) (including answers)
The art of Engineering (3): do not rely on each other between functions of code robustness
Jerry's setting currently uses the dial. Switch the dial through this function [chapter]
重磅硬核 | 一文聊透对象在 JVM 中的内存布局,以及内存对齐和压缩指针的原理及应用
李書福為何要親自掛帥造手機?
Getting started with pytest ----- allow generate report
Distinguish between basic disk and dynamic disk RAID disk redundant array
Zen integration nails, bugs, needs, etc. are reminded by nails
Pytest learning ----- detailed explanation of the request for interface automation test
Easy introduction to SQL (1): addition, deletion, modification and simple query