当前位置:网站首页>二分(整数二分、实数二分)
二分(整数二分、实数二分)
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;
}
边栏推荐
- HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
- F200——搭载基于模型设计的国产开源飞控系统无人机
- Single responsibility principle
- MySQL error reporting solution
- Spark calculation operator and some small details in liunx
- EasyCVR平台通过接口编辑通道出现报错“ID不能为空”,是什么原因?
- MySQL stored procedure
- BearPi-HM_ Nano development environment
- EasyCVR授权到期页面无法登录,该如何解决?
- 编译原理——预测表C语言实现
猜你喜欢

Grafana 9 正式发布,更易用,更酷炫了!

Pytest learning ----- pytest operation mode and pre post packaging of interface automation testing

Basic configuration and use of spark

2022年大厂Android面试题汇总(一)(含答案)

面试突击62:group by 有哪些注意事项?
![[rapid environment construction] openharmony 10 minute tutorial (cub pie)](/img/b5/feb9c56a65c3b07403710e23078a6f.jpg)
[rapid environment construction] openharmony 10 minute tutorial (cub pie)

C语言通过指针交换两个数

基于STM32+华为云IOT设计的智能路灯

FlutterWeb浏览器刷新后无法回退的解决方案

Olivetin can safely run shell commands on Web pages (Part 1)
随机推荐
Stealing others' vulnerability reports and selling them into sidelines, and the vulnerability reward platform gives rise to "insiders"
MarkDown语法——更好地写博客
MySQL error reporting solution
TCP connection is more than communicating with TCP protocol
Debug and run the first xv6 program
Chrome prompts the solution of "your company management" (the startup page is bound to the company's official website and cannot be modified)
Solution qui ne peut pas être retournée après la mise à jour du navigateur Web flutter
OpenCV中如何使用滚动条动态调整参数
Pytest learning ----- pytest operation mode and pre post packaging of interface automation testing
编译原理——预测表C语言实现
How to output special symbols in shell
What is the reason why the video cannot be played normally after the easycvr access device turns on the audio?
【MySQL入门】第三话 · MySQL中常见的数据类型
Growth of operation and maintenance Xiaobai - week 7
How to submit data through post
分布式不来点网关都说不过去
Pytorch extract middle layer features?
Cool Lehman has a variety of AI digital human images to create a vr virtual exhibition hall with a sense of technology
面试突击63:MySQL 中如何去重?
Interview assault 63: how to remove duplication in MySQL?