当前位置:网站首页>Binary search (integer binary)
Binary search (integer binary)
2022-06-22 15:47:00 【Stephen_ Curry___】
Two points search
Binary search, as the name suggests, is a binary search , The most difficult problem in the bisection algorithm is the boundary problem .int l=0,r=n-1;
Generally, there are two methods to take the boundary : Rounding down mid=(l+r)/2 And rounding up mid=(l+r+1)/2;
The core code boards of the two boundary methods are as follows :
//1, Rounding down
int one(int l,int r)
{
while(l<r)
{
int mid=(l+r)/2;
if(chack(mid)==true)r=mid;
else l=mid+1;
}
return l;
}
//2. Rounding up
int tow(int l,int r)
{
while(l,r)
{
int mid=(l+r+1)/2;
if(check(mid)==true)l=mid;
else r=mid-1;
}
return l;
}
When rounded down mid=(l+r)/2 when check(mid) Whether the conditions are met , If Meet the conditions It means that the answer to be searched is in the interval [l,mid] Within the interval , to update r=mid ; If Not meeting the conditions It means that the answer is in the range [mid+1,r] Inside , to update l=mid+1.
When rounded up mid=(l+r+1)/2 when check(mid) Whether the conditions are met , If Meet the conditions It means that the answer to be searched is in the interval [mid,r] Within the interval , to update l=mid; If Not meeting the conditions It means that the answer is in the range [l,mid-1] Inside , to update r=mid-1.
Take a chestnut : Orderly ascending array a[0]~a[7] Respectively 2 3 4 5 6 7 8 9
We need to look it up x=5 First occurrence , namely a[3]; We need to judge a[i]<=x;

1. When mid=(l+r)/2 when

here a[mid]=5, Meet the conditions , We're going to update r=mid
2. When mid=(l+r+1)/2 when

here a[mid]=6, Not meeting the conditions , We're going to update r=mid-1.
Here is a classic binary search question
Input
Input the first line of test data n,m Indicates the length of the array and the number of queries (1<=n,m<=105)
The next line is n A digital A0 . . . An-1 Satisfy Ai<=Ai+1 (1<=Ai<=109)
Then input m That's ok , Each line is a number Qi Asking (1<=Qi<=109)
Output
For each inquiry , If Qi In an array , Output the position of the first occurrence and the last occurrence of two numbers respectively , If Qi It doesn't appear in the array , The output “-1 -1”
Example
Input
10 3
3 3 3 5 5 5 6 7 7 7
5
6
10
Output
3 5
6 6
-1 -1
The code is as follows
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1e6 + 10;
int n, m;
int a[N];
int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)scanf("%d",&a[i]);
while(m--)
{
int x;
scanf("%d",&x);
int l=0,r=n-1;
while(l<r)
{
int mid=l+r>>1;//mid=(l+r)/2;
if(a[mid]>=x)r=mid;
else l=mid+1;
}
if(a[l]!=x)cout<<"-1 -1"<<endl;
else
{
cout<<l<<" ";// Output left boundary
int l=0,r=n-1;// Next, find the right boundary
while(l<r)
{
int mid=l+r+1>>1;//mid=(l+r+1)/2;
if(a[mid]<=x)l=mid;
else r=mid-1;
}
cout<<l<<endl;
}
}
}
The above is the integer binary search content in binary search , I believe you have a deeper understanding of dichotomy , Practice is the only criterion for testing truth , Brush questions to consolidate !!!
If you have any questions, please don't hesitate to comment orn.
边栏推荐
- C语言学生成绩排名系统
- How can ordinary people make 1million yuan a year?
- 晒晒我这两年的私活单,业余时间月入6k,有份副业也太香啦
- 向量3(静态成员)
- 极致效率,云原生数据库TDSQL-C安身立命的根本
- Advanced thinking on application scenarios of standardization, maximum normalization and mean normalization
- Promouvoir l'adaptation compatible et permettre le développement collaboratif du Service Express adaptatif gbase en mai
- 专业“搬砖”老司机总结的 12 条 SQL 优化方案,非常实用!
- Rosbag使用命令
- HMS Core新闻行业解决方案:让技术加上人文的温度
猜你喜欢
米哈游六月社招火热开启!500+岗位,超多HC,就在这个夏天(附内推方式)

The 12 SQL optimization schemes summarized by professional "brick moving" old drivers are very practical!

二分查找(整数二分)

关于 GIN 的路由树

(pytorch进阶之路二)word embedding 和 position embedding

Promoting compatibility and adaptation, enabling coordinated development of gbase may adaptation Express

Please, don't be brainwashed. This is the living reality of 90% of Chinese people
![Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;](/img/75/d2ad171d49611a6578faf2d390af29.jpg)
Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;

FreeRTOS task priority and interrupt priority

Database connection pool: stress testing
随机推荐
Rosbag使用命令
Is pioneer futures reliable? How to open a futures account safely?
快速排序quick_sort
MongoDB在腾讯零售优码中的应用
HMS Core新闻行业解决方案:让技术加上人文的温度
Mitsubishi manipulator demo program
New hybrid architecture iformer! Flexible migration of convolution and maximum pooling to transformer
SDVO:LDSO+语义,直接法语义SLAM(RAL 2022)
类似attention nlp
ROS2前置基础教程 | 使用CMakeLists.txt编译ROS2节点
The MIHA tour club in June is hot! 500+ posts, more than HC, just this summer (with internal promotion method)
百行代码实现基于Redis的可靠延迟队列
Scala语言学习-05-递归和尾递归效率对比
『忘了再学』Shell流程控制 — 38、while循环和until循环介绍
Exploration and practice of dewu app data simulation platform
GBASE现身说 “库” 北京金融科技产业联盟创新应用专委会专题培训
三菱机械臂demo程序
C language learning -18-makefile file writing examples and how to generate and call dynamic libraries
向量6(继承)
Is the encryption market a "natural disaster" or a "man-made disaster" in the cold winter?