Advertisement
messh

Untitled

Jul 9th, 2025
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5. #define ll long long
  6. #define int long long
  7. const int inf = 1e18;
  8.  
  9. ll M2 = 1e9 + 9, M1 = 1e9 + 7;
  10. //mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
  11.  
  12. void solve() {
  13.     int n, m;
  14.     cin >> n >> m;
  15.     vector<int> a(n);
  16.     vector<int> b(m);
  17.     for (auto &x : a) {
  18.         cin >> x;
  19.     }
  20.     for (auto &x : b) {
  21.         cin >> x;
  22.         int l = 0, r = n;
  23.         while (r - l > 1) {
  24.             int mid = (r + l) / 2;
  25.             if (a[mid] > x) {
  26.                 r = mid;
  27.             } else {
  28.                 l = mid;
  29.             }
  30.         }
  31.  
  32.         int mn = inf, res = inf;
  33.         for (int i = l; i <= min(n - 1, r); ++i) {
  34.             if (abs(a[i] - x) < mn) {
  35.                 mn = abs(a[i] - x);
  36.                 res = a[i];
  37.             }
  38.         }
  39.         cout << res << '\n';
  40.     }
  41.  
  42.  
  43.  
  44. }
  45.  
  46. signed main() {
  47.  
  48.  
  49.     ios_base::sync_with_stdio(false);
  50.     cin.tie(nullptr);
  51.     ll thrush = 1;
  52.     //cin >> thrush;
  53.     while (thrush--) { solve();}
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement