Advertisement
libobil

Untitled

Oct 21st, 2023
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <numeric>
  4. #include <cassert>
  5. #include <vector>
  6.  
  7. typedef long long llong;
  8. const int MAXN = 100000 + 10;
  9. const llong INF = 1e18;
  10. const int INTINF = 1e9;
  11.  
  12. int n, q;
  13. int a[MAXN];
  14.  
  15. void solve()
  16. {
  17.     std::sort(a + 1, a + 1 + n);
  18.     for (int i = 1 ; i <= q ; ++i)
  19.     {
  20.         int value;
  21.         std::cin >> value;
  22.         int l = 0, r = n + 1, mid; //при интервал [start, end], l = start - 1, r = end + 1 за хващане на гранични случаи. В самото binary не се изчисляват неща за start - 1 и end + 1
  23.         while (l < r - 1)
  24.         {
  25.             mid = (l + r) / 2;
  26.             if (a[mid] <= value) l = mid;
  27.             else r = mid;
  28.         }
  29.  
  30.         if (l == 0) std::cout << -1 << '\n';
  31.         else std::cout << a[l] << '\n';
  32.     }
  33. }
  34.  
  35. void input()
  36. {
  37.     std::cin >> n >> q;
  38.     for (int i = 1 ; i <= n ; ++i)
  39.     {
  40.         std::cin >> a[i];
  41.     }
  42. }
  43.  
  44. void fastIOI()
  45. {
  46.     std::ios_base :: sync_with_stdio(0);
  47.     std::cout.tie(nullptr);
  48.     std::cin.tie(nullptr);
  49. }
  50.  
  51. int main()
  52. {
  53.     fastIOI();
  54.     input();
  55.     solve();
  56.    
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement