当前位置:网站首页>Range of numbers (dichotomous classic template topic)
Range of numbers (dichotomous classic template topic)
2022-04-23 05:33:00 【Ice Cream~】
Given a length in ascending order is n Array of integers for , as well as q A query .
For each query , Returns an element k Starting and ending positions of ( Location slave 0 Start counting ).
If the element does not exist in the array , Then return to
-1 -1
.Input format
The first line contains integers n and q, Represents the length of the array and the number of queries .
The second line contains n It's an integer ( Both in 1∼10000 Within the scope of ), Represents a complete array .
Next q That's ok , Each line contains an integer k, Represents a query element .
Output format
common q That's ok , Each line contains two integers , Represents the starting and ending positions of the desired element .
If the element does not exist in the array , Then return to
-1 -1
.Data range
1≤n≤100000
1≤q≤10000
1≤k≤10000sample input :
6 3 1 2 2 3 3 4 3 4 5
sample output :
3 4 5 5 -1 -1
Ideas : This topic belongs to Integer binary search A classic example of , For an ordered sequence of numbers , lookup x Start address and end address , You can find out the first ratio through dichotomy x The position of a large number a[mid]>=x, Then search from the last position to... Through two points n-1 Mid position ratio x The small first number a[mid]<=x.
Code :
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int a[100010];
int main()
{
int n,m;cin>>n>>m;
for(int i=0;i<n;i++)cin>>a[i];
while(m--)
{
int x;cin>>x;
// Two points search , Defect search interval
int l=0,r=n-1;
while(l<r)
{
int mid=l+r>>1;
if(a[mid]>=x){ // Find the first one greater than x The location of r
r=mid;
}
else l=mid+1;
}
int ll=r,rr=n-1;
while(ll<rr)
{
int mid=ll+rr+1>>1;
if(a[mid]<=x){ // Find the first one less than x The location of rr
ll=mid;
}
else rr=mid-1;
}
if(a[r]==x)
{
cout<<r<<" "<<ll<<endl;
}
else cout<<"-1 -1"<<endl;
}
return 0;
}
版权声明
本文为[Ice Cream~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220534524889.html
边栏推荐
- TSlint注释忽略错误和RESTful理解
- Usage and difference of shellexecute, shellexecuteex and winexec in QT
- Introduction to qqueue
- Interview Basics
- catkin_ What did package do
- 世界与个人发展
- Camera imaging + homography transformation + camera calibration + stereo correction
- Call the interface to get the time
- 史上最强egg框架的error处理机制
- [machine learning] scikit learn introduction
猜你喜欢
Pytorch deep learning practice_ 11 convolutional neural network
Arithmetic and logical operations
QT displays the specified position and size of the picture
Intel SGX preliminary learning and understanding notes (continuously updated)
Camera imaging + homography transformation + camera calibration + stereo correction
Understand the relationship between promise async await
Cross domain CORS relationship~
[untitled] Notepad content writing area
[the background color changes after clicking a line]
弘玑Cyclone RPA为国金证券提供技术支撑,超200个业务场景实现流程自动化
随机推荐
Escape characters \ splicing of data formats
IPI interrupt
史上最强egg框架的error处理机制
MySQL series - install MySQL 5.6.27 on Linux and solve common problems
Frequently asked interview questions - 1 (non technical)
Double click The jar package cannot run the solution
How to realize adaptive layout
SQL语句简单优化
Frequently asked interview questions - 3 (operating system)
Interview Basics
Call the interface to get the time
Hongji cyclone RPA provides technical support for Guojin securities and realizes process automation in more than 200 business scenarios
Differences between auto and decltype inference methods (learning notes)
(十一)vscode代码格式化配置
Parsing of string class intern() method
五一劳动节期间什么理财产品会有收益?
Markdown syntax support test
The main difference between pointer and reference
Quick app bottom navigation bar
Use of ES6 array