当前位置:网站首页>二分(整数二分、实数二分)
二分(整数二分、实数二分)
2022-07-06 10:01:00 【Stellaris_L】
二、二分
单调序列或单调函数中的查找方式。(终止条件:l=r)
整数域二分
解析
- 注意终止边界、左右区间取舍。
(1)缩小范围:r=mid,l=mid+1,取中间值:mid=(l+r)>>1。
(2)缩小范围:l=mid,r=mid+r,取中间值:mid=(l+r+1)>>1。
第一种不会取到 r r r这个值,第二种不会取到 l l l。可以用来处理无解的情况。最初的二分区间从[1,n]扩大到[1,n+1]和[0,n],若最后二分终止在这个越界的下标上,则无解。
huangzh
- 右移运算向下取整,整数除法向零取整。
模板
例题
#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;
}
实数域二分
解析
- 注意精度问题
使用循环固定次数的二分方法解决精度问题。(精度比eps高)
模板
for(int i=0;i<100;i++){
//求mid
double mid=(l+r)/2;
if(calc(mid))r=mid; else l=mid;
}
例题
- eps的精度要求
#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;
}
边栏推荐
- BearPi-HM_ Nano development environment
- What is the reason why the video cannot be played normally after the easycvr access device turns on the audio?
- Getting started with pytest ----- test case rules
- Essai de pénétration du Code à distance - essai du module b
- Solid principle
- The art of Engineering (2): the transformation from general type to specific type needs to be tested for legitimacy
- connection reset by peer
- The shell generates JSON arrays and inserts them into the database
- 2022年大厂Android面试题汇总(一)(含答案)
- Nodejs 开发者路线图 2022 零基础学习指南
猜你喜欢
10 advanced concepts that must be understood in learning SQL
Why should Li Shufu personally take charge of building mobile phones?
李书福为何要亲自挂帅造手机?
Sqoop I have everything you want
RB157-ASEMI整流桥RB157
F200——搭载基于模型设计的国产开源飞控系统无人机
Grafana 9 is officially released, which is easier to use and more cool!
ASEMI整流桥DB207的导通时间与参数选择
2022年大厂Android面试题汇总(一)(含答案)
【Elastic】Elastic缺少xpack无法创建模板 unknown setting index.lifecycle.name index.lifecycle.rollover_alias
随机推荐
2022年大厂Android面试题汇总(二)(含答案)
Alibaba brand data bank: introduction to the most complete data bank
Concept and basic knowledge of network layering
Vscode matches and replaces the brackets
面试突击62:group by 有哪些注意事项?
学 SQL 必须了解的 10 个高级概念
Single responsibility principle
Xin'an Second Edition: Chapter 24 industrial control safety demand analysis and safety protection engineering learning notes
connection reset by peer
视频融合云平台EasyCVR增加多级分组,可灵活管理接入设备
8位MCU跑RTOS有没有意义?
After entering Alibaba for the interview and returning with a salary of 35K, I summarized an interview question of Alibaba test engineer
VR全景婚礼,帮助新人记录浪漫且美好的场景
EasyCVR授权到期页面无法登录,该如何解决?
Automatic operation and maintenance sharp weapon ansible Playbook
Quick start of Hongmeng system
历史上的今天:Google 之母出生;同一天诞生的两位图灵奖先驱
Solution qui ne peut pas être retournée après la mise à jour du navigateur Web flutter
EasyCVR平台通过接口编辑通道出现报错“ID不能为空”,是什么原因?
Cool Lehman has a variety of AI digital human images to create a vr virtual exhibition hall with a sense of technology