fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. int main() {
  7. int N, K;
  8. cin >> N >> K;
  9.  
  10. vector<int> a(N);
  11. for (int i = 0; i < N; i++) {
  12. cin >> a[i];
  13. }
  14.  
  15. vector<int> b(K);
  16. for (int i = 0; i < K; i++) {
  17. cin >> b[i];
  18. }
  19.  
  20. for (int i = 0; i < K; i++) {
  21. int x = b[i];
  22. auto d = lower_bound(a.begin(), a.end(), x);
  23. int h = d - a.begin();
  24. int g = a[h];
  25. if (h > 0 && abs(a[h - 1] - x) <= abs(a[h] - x)) {
  26. g = a[h - 1];
  27. }
  28. cout << g << endl;
  29. }
  30.  
  31. return 0;
  32. }
  33.  
Success #stdin #stdout 0.01s 5276KB
stdin
5 5
1 3 5 7 9 
2 4 8 1 6 
stdout
1
3
7
1
5