当前位置:网站首页>[c language] limit the number of searches and output the maximum value found in the number of internal searches
[c language] limit the number of searches and output the maximum value found in the number of internal searches
2022-06-11 18:17:00 【Dancing Pierre】
[C Language ] Limit the number of lookups , The maximum value found in the output times
1、 subject
Give you a length of n Array of A. Ask you m Time : In the array A in , Less than or equal to a given number xi What is the maximum value of ?
Input format
- first line : Two integers n and m, Represents the length of the array and the number of lookups .
- The second line :n It's an integer ai, Represents each number in the array .
- Next m That's ok , Each row has 1 It's an integer xi , Integer representing lookup .
Output format
- For each query , If you can find , The output is less than or equal to xi The maximum of .
- Otherwise output -1.
Examples :
Input :
10 4
1 2 3 3 3 5 5 7 7 9
0
4
9
10
Output :
-1
3
9
9
2、 Complete code
#include <iostream>
#include <algorithm>
using namespace std;
int n, m;
int a[100001];
int judge(int input,int a[]) {
int res;
// Query cannot find direct output -1
if (input < a[0]) {
res = -1;
}
// If the number of lookups is longer than the array , Output the last number of sorted array directly
else if (input >= a[n - 1]) {
res = a[n - 1];
}
else if (input == a[0]) {
res = a[0];
}
// Two points search
int ans = -1;
int left = 0, right = n - 1;
while (left <= right) {
// Moves to the right one
int mid = (left + right) >> 1;
if (a[mid] <= input) {
ans = a[mid];
left = mid + 1;
}
else {
right = mid - 1;
}
}
res = ans;
return res;
}
int main() {
// Array length and number of lookups
cin >> n >> m;
// Enter full array
for (int i = 0; i < n; i++) {
cin >> a[i];
}
// Array sorting , Default ascending order
sort(a, a + n);
for (int i = 0; i < m; i++) {
int x;
// Enter the number you want to find
cin >> x;
cout << judge(x, a) << endl;
}
return 0;
}
3、 Screenshot

边栏推荐
- 最长严格递增子序列
- [golang] leetcode - 292 Nim games (Mathematics)
- Global and Chinese markets for private internet access 2022-2028: Research Report on technology, participants, trends, market size and share
- [C语言]用结构体把输入的指定分数范围内的学生输出
- [collect first and use it sooner or later] 49 Flink high-frequency interview questions series (I)
- [C语言]对一个数组的元素排序后平移元素
- Hello go (XIV). Go language common standard library IV
- 神经网络与深度学习-2- 机器学习简单示例-PyTorch
- [c language] output students' names and scores in descending order of scores with structures
- Cryptology Summary
猜你喜欢

網絡安全威脅情報體系
![Spring 2021 daily question [week5 not finished]](/img/bd/35a8e0ded3b1a0727415c4cd95e781.jpg)
Spring 2021 daily question [week5 not finished]

Introduction to social engineering practice

NFT platform development NFT mall source code NFT mall development chain game development

SISO decoder for a general (n, n-1) SPC code (supplementary Chapter 3)

SISO Decoder for a General (n, N - 1) SPC Code (Supplementary section 3)

如何学习和自学

SISO decoder for min sum (supplementary Chapter 2)
![[C语言]对一个数组的元素排序后平移元素](/img/5b/3e74fc40787d94f6d0ab93332140ba.png)
[C语言]对一个数组的元素排序后平移元素

求字符串中最大的 3 位相同数字
随机推荐
安全领域常规术语
系统的可扩展型
EditText 金额限制
Secret comment-----
在同花顺上面开股票账户好不好,安不安全?
【MapReduce】一个完整MR程序案例教你如何用IDEA打包及运行
合并多棵二叉搜索树
sqli-labs通关嘿嘿~
Easycwmp source code analysis
ISCSI详解(四)——ISCSI服务端配置实战
如何学习和自学
Système d'information sur les menaces à la sécurité des réseaux
RadioGroup动态添加RadioButton
软件需求工程复习
Acwing game 40 [End]
Reading summary of nacos2.x source code
Jsfinder, wafw00f installation, nmap configuration (msvcr120.dll file is missing)
Talking about telework | community essay solicitation
社会工程学实战入门
NFT platform development NFT mall source code NFT mall development chain game development