当前位置:网站首页>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;
}
边栏推荐
- 一体化实时 HTAP 数据库 StoneDB,如何替换 MySQL 并实现近百倍性能提升
- Jielizhi obtains the customized background information corresponding to the specified dial [chapter]
- Codeforces Round #803 (Div. 2)
- MarkDown语法——更好地写博客
- Fleet tutorial 13 basic introduction to listview's most commonly used scroll controls (tutorial includes source code)
- Mysqlimport imports data files into the database
- Unity particle special effects series - treasure chest of shining stars
- Kivy tutorial: support Chinese in Kivy to build cross platform applications (tutorial includes source code)
- The art of Engineering
- 编译原理——预测表C语言实现
猜你喜欢
node の SQLite
C # nanoframework lighting and key esp32
Sqoop I have everything you want
历史上的今天:Google 之母出生;同一天诞生的两位图灵奖先驱
Getting started with pytest ----- allow generate report
Getting started with pytest ----- test case rules
OpenCV中如何使用滚动条动态调整参数
Getting started with pytest ----- test case pre post, firmware
Today in history: the mother of Google was born; Two Turing Award pioneers born on the same day
编译原理——预测表C语言实现
随机推荐
Sqoop I have everything you want
Distributed (consistency protocol) leader election (dotnext.net.cluster implements raft election)
重磅硬核 | 一文聊透对象在 JVM 中的内存布局,以及内存对齐和压缩指针的原理及应用
8位MCU跑RTOS有没有意义?
scratch疫情隔离和核酸检测模拟 电子学会图形化编程scratch等级考试三级真题和答案解析2022年6月
10 advanced concepts that must be understood in learning SQL
FlutterWeb浏览器刷新后无法回退的解决方案
The art of Engineering (3): do not rely on each other between functions of code robustness
开源与安全的“冰与火之歌”
Hongmeng introduction and development environment construction
历史上的今天:Google 之母出生;同一天诞生的两位图灵奖先驱
BearPi-HM_ Nano development board "flower protector" case
Getting started with pytest ----- allow generate report
Today in history: the mother of Google was born; Two Turing Award pioneers born on the same day
Unity tips - draw aiming Center
OliveTin能在网页上安全运行shell命令(上)
编译原理——预测表C语言实现
Codeforces Round #803 (Div. 2)
Awk command exercise
最新财报发布+天猫618双榜第一,耐克蓄力领跑下个50年