当前位置:网站首页>7-1 range of numbers
7-1 range of numbers
2022-06-26 13:23:00 【White -】
7-1 The range of numbers
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 .
1≤n≤100000
1≤q≤10000
1≤k≤10000
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.
sample input :
Here's a set of inputs . for example :
6 3
1 2 2 3 3 4
3
4
5
sample output :
Here is the corresponding output . for example :
3 4
5 5
-1 -1
Code :
#include <stdio.h>
#include <stdlib.h>
int n,q;
int x;
int a[100010];
int start,end;
void findsande(int mid)
{
int start=mid,end=mid;
for(int i=mid;i>=0;i--)
{
if(a[i]!=x)
{
start=i+1;
break;
}
}
for(int i=mid;i<=n-1;i++)
{
if(a[i]!=x)
{
end=i-1;
break;
}
}
printf("%d %d\n",start,end);
}
int find(int left,int right)
{
if(left>right)
return -1;
int mid=(left+right)/2;
if(a[mid]==x)
{
findsande(mid);
return 1;
}
else if(x>a[mid])
find(mid+1,right);
else
find(left,mid-1);
}
int main()
{
scanf("%d%d",&n,&q);
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(int i=0;i<q;i++)
{
scanf("%d",&x);
if(find(0,n-1)==-1)
printf("-1 -1\n");
}
return 0;
}
202206260911 Japan
边栏推荐
- MySQL数据库讲解(五)
- KITTI Detection dataset whose format is letf_ top_ right_ bottom to JDE normalied xc_ yc_ w_ h
- Opencv high speed download
- MySQL数据库讲解(四)
- 8. [STM32] timer (TIM) -- interrupt, PWM, input capture experiment (proficient in timer)
- Ring queue PHP
- G - Cow Bowling
- Beifu PLC based on NT_ Shutdown to realize automatic shutdown and restart of controller
- Generate JDE dot train
- mariadb学习笔记
猜你喜欢

Some conclusions about Nan

scrapy——爬取漫画自定义存储路径下载到本地

Processsing function random

Oplg: new generation cloud native observable best practices
Summary of wechat applet test points

Beifu PLC model selection -- how to see whether the motor is a multi turn absolute value encoder or a single turn absolute value encoder

5月产品升级观察站

Bigint: handles large numbers (integers of any length)

Ring queue PHP

Processsing mouse interactive learning
随机推荐
Machine learning notes - seasonality of time series
Enjoy element mode (flyweight)
POJ 3070 Fibonacci
Bridge mode
桥接模式(Bridge)
MySQL explanation (II)
组合模式(Composite )
Luogu p3426 [poi2005]sza-template solution
Learn how to develop owl components by hand (7): practical use of owl projects
5+api, clear application cache
Learning Processing Zoog
MySQL数据库讲解(三)
IDC报告:百度智能云AI Cloud市场份额连续六次第一
Map value
H - Sumsets POJ 2229
Arcpy -- use of insertlayer() function: adding layers to map documents
To solve the difficulties of small and medium-sized enterprises, Baidu AI Cloud makes an example
Decorator
Script - crawl the customized storage path of the cartoon and download it to the local
A primary multithreaded server model