描述
给出一个大小为n的数组a,有m次询问,每次询问给出一个x,你需要输出数组a中大于x的最小值,如果不存在,输出-1。
要求使用set实现。
输入描述:
第一行两个整数n ,m。
第二行nn个整数表示数组a。
接下来m行,每行一个整数x。
输出描述:
对每个询问,输出一行一个整数表示答案,如果不存在输出-1。
示例1
输入:
5 5 6 3 8 3 5 6 9 7 7 3
输出:
8 -1 8 8 5
#include<iostream>
#include<set>
using namespace std;
int main() {
set<int>s;
//write your code here......
int n, m;
cin >> n >> m;
int num = 0;
for (int i = 0; i < n; i++) {
cin >> num;
s.insert(num);
}
for (int i = 0; i < m; i++) {
cin >> num;
auto it = s.upper_bound(num);
if (it==s.end())
{
cout << -1 << endl;
}
else
{
cout << *it << endl;
}
}
return 0;
}