题解 | #查找#
查找
https://www.nowcoder.com/practice/8e0c0e8c78944847be9bca54b59d713f
#include <iostream> #include<bits/stdc++.h> using namespace std; int main(){ set<int>s; //write your code here...... int n; int m; cin>>n; cin>>m; for(int i=0;i<n;++i){ int num; cin>>num; s.insert(num); } int arr_x[m]; int arr_out[m]; for(int i=0;i<m;++i){ cin>>arr_x[i]; } for(int i=0;i<m;++i){ // bool find=false; // for(set<int>::iterator itr = s.begin();itr != s.end();++itr){ // if(arr_x[i] < *itr){ // arr_out[i] = *itr; // find = true; // break; // } // } // if(!find) arr_out[i] = -1; auto itr = s.upper_bound(arr_x[i]); if(itr != s.end()){ arr_out[i] = *itr; }else{ arr_out[i] = -1; } } for(int i=0;i<m;++i){ cout<<arr_out[i]<<endl; } return 0; }