当前位置:网站首页>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 5sample 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
边栏推荐
- what is wifi6?
- Some pits used by uni
- Escape characters \ splicing of data formats
- Three methods of list rendering
- d. TS --- for more detailed knowledge, please refer to the introduction on the official website (chapter of declaration document)
- 世界与个人发展
- Excel 2016 cannot open the file for the first time. Sometimes it is blank and sometimes it is very slow. You have to open it for the second time
- Fast application fuzzy search
- Output string in reverse order
- 踩坑:nacos利用startup.cmd -m standalone启动错误
猜你喜欢

Hongji | how does HR carry out self change and organizational change in the digital era?

Use of ES6 array

Traversal array, object parent-child communication props / $emit
Redis的基本知识

Use of uniapp native plug-ins

STL learning notes 0x0001 (container classification)

After adding qmenu to qtoolbutton and QPushButton, remove the triangle icon in the lower right corner

如果我是pm之 演出电影vr购票展示

Deep learning object detection

SQL语句简单优化
随机推荐
Self incrementing sequence creation of MySQL
Note: unordered_ Understanding and use of map
Frequently asked interview questions - 1 (non technical)
[triangle Yang Hui triangle printing odd even cycle JS for break cycle]
Add two pointers? (legal or illegal)
Simple and basic use of switch and if
Parameter analysis of open3d material setting
STL learning notes 0x0001 (container classification)
Xiuxian real world and game world
JVM memory and memory overflow exceptions (personal summary)
egg中的cors和proxy(づ ̄3 ̄)づ╭~踩坑填坑的过程~ToT~
Output string in reverse order
FileReader API file operation
Call the interface to get the time
五一劳动节期间什么理财产品会有收益?
CMake基础教程(39)pkgconfig
Create cells through JS (while loop)
如果我是pm之 演出电影vr购票展示
Generation of straightening body in 3D slicer
npm升级后问题,慌得一批