当前位置:网站首页>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;
}
边栏推荐
- VR全景婚礼,帮助新人记录浪漫且美好的场景
- Shell input a string of numbers to determine whether it is a mobile phone number
- IP, subnet mask, gateway, default gateway
- Solid principle
- There is a gap in traditional home decoration. VR panoramic home decoration allows you to experience the completion effect of your new house
- EasyCVR接入设备开启音频后,视频无法正常播放是什么原因?
- RB157-ASEMI整流桥RB157
- [elastic] elastic lacks xpack and cannot create template unknown setting index lifecycle. name index. lifecycle. rollover_ alias
- RepPoints:可形变卷积的进阶
- node の SQLite
猜你喜欢
Distributed (consistency protocol) leader election (dotnext.net.cluster implements raft election)
Smart street lamp based on stm32+ Huawei cloud IOT design
Establishment of graphical monitoring grafana
历史上的今天:Google 之母出生;同一天诞生的两位图灵奖先驱
Heavy! Ant open source trusted privacy computing framework "argot", flexible assembly of mainstream technologies, developer friendly layered design
The solution that flutterweb browser cannot be rolled back after refreshing
编译原理——自上而下分析与递归下降分析构造(笔记)
Is it meaningful for 8-bit MCU to run RTOS?
node の SQLite
How to solve the error "press any to exit" when deploying multiple easycvr on one server?
随机推荐
Jerry's watch reads the file through the file name [chapter]
Kivy tutorial: support Chinese in Kivy to build cross platform applications (tutorial includes source code)
Today in history: the mother of Google was born; Two Turing Award pioneers born on the same day
C # nanoframework lighting and key esp32
Smart street lamp based on stm32+ Huawei cloud IOT design
最新财报发布+天猫618双榜第一,耐克蓄力领跑下个50年
高精度运算
Pytest learning ----- pytest operation mode and pre post packaging of interface automation testing
Debug xv6
The art of Engineering (1): try to package things that do not need to be exposed
JMeter interface test response data garbled
Open source and safe "song of ice and fire"
EasyCVR接入设备开启音频后,视频无法正常播放是什么原因?
一体化实时 HTAP 数据库 StoneDB,如何替换 MySQL 并实现近百倍性能提升
视频融合云平台EasyCVR增加多级分组,可灵活管理接入设备
[ASM] introduction and use of bytecode operation classwriter class
How to use scroll bars to dynamically adjust parameters in opencv
Interview shock 62: what are the precautions for group by?
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
FlutterWeb瀏覽器刷新後無法回退的解决方案