当前位置:网站首页>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 database explanation (IV)
- Nlp-d60-nlp competition D29
- 首批通过!百度智能云曦灵平台获信通院数字人能力评测权威认证
- 7-1 n queen problem
- Beifu PLC obtains system time, local time, current time zone and system time zone conversion through program
- KITTI Detection dataset whose format is letf_ top_ right_ bottom to JDE normalied xc_ yc_ w_ h
- Electron official docs series: References
- Arcpy——InsertLayer()函數的使用:摻入圖層到地圖文檔裏
- Enjoy element mode (flyweight)
- ES中索引别名(alias)的到底有什么用
猜你喜欢

Analysis of state transition diagram of Beifu NC axis

Electron official docs series: Processes in Electron

Mode pont

MySQL讲解(二)

Some conclusions about Nan

Common faults of MySQL database - forgetting database password

外观模式(Facade)

Beifu PLC realizes data power-off maintenance based on cx5130

Fire warning is completed within 10 seconds, and Baidu AI Cloud helps Kunming Guandu build a new benchmark of smart city

MySQL数据库讲解(三)
随机推荐
[how to connect the network] Chapter 2 (Part 1): establish a connection, transmit data, and disconnect
【Spark】. Explanation of several icons of scala file in idea
Common creation and usage of singletons
Stack, LIFO
Nlp-d60-nlp competition D29
Detailed practical sharing, two hours of funny videos after work, earning more than 7000 a month
scrapy——爬取漫画自定义存储路径下载到本地
G - Cow Bowling
10秒内完成火灾预警,百度智能云助力昆明官渡打造智慧城市新标杆
防火墙介绍
A collection of common tools for making we media videos
Electron official docs series: Examples
SQL assigns the field value of data table B to a column in data table a
8. [STM32] timer (TIM) -- interrupt, PWM, input capture experiment (proficient in timer)
Processsing mouse interactive learning
es常用语法一
awk工具
Mysql database explanation (III)
Ring queue PHP
HDU 3555 Bomb