Guest User

Untitled

a guest
Jan 24th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <stdio.h>
  5.  
  6. using std::vector;
  7. using std::cin;
  8. using std::cout;
  9. using std::endl;
  10.  
  11. typedef long long ll;
  12.  
  13. int lower_bound(vector<int> * vec, int val)
  14. {
  15. if((*vec).size() == 1) return 0;
  16. int l = 0, r = (*vec).size() - 1;
  17.  
  18. while(r - l > 1)
  19. {
  20. int mid = (l + r) / 2;
  21.  
  22. if((*vec)[mid] < val) l = mid;
  23. else r = mid-1;
  24. }
  25. if((*vec)[l] < val && (*vec)[l+1] <= val) return l+1;
  26. return l;
  27. }
  28.  
  29. int main()
  30. {
  31. //n - количество элементов в массиве(он упорядочен по неубыванию)
  32. //m - количество запросов
  33.  
  34. int n
  35. cin >> n >> m;
  36. vector<int> v(n);
  37. for(int i = 0; i < n; i++) cin >> v[i];
  38.  
  39. for(; m > 0; m--)
  40. {
  41. int val;
  42. cin >> val;
  43.  
  44. int pos = lower_bound(&v, val);
  45. cout << v[pos] << endl;
  46. }
  47. return 0;
  48. }
  49.  
  50. if ((*vec)[r] - val >= val - (*vec)[l])
  51. return l;
  52. return r;
Add Comment
Please, Sign In to add comment