Advertisement
Korotkodul

C. Приближенный двоичный поиск

Nov 15th, 2021
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <set>
  5. #include <string>
  6. #include <algorithm>
  7. using namespace std;
  8. using ll = long long;
  9. void co(vector <int> v){
  10. for (auto x: v) cout<<x<<'\n';
  11. cout<<'\n';
  12. }
  13. vector <int> A(1);
  14.  
  15. void bp(int x){
  16. int R = A.size();
  17. int L = 0;
  18. while (R - L > 1){
  19. int M = (R + L ) / 2;
  20. if (A[M] > x){
  21. R = M;
  22. }else{
  23. L = M;
  24. }
  25. }
  26. //cout<<A[L] <<' '<<A[R]<<'\n';
  27. if (abs(x - A[L]) <= abs(x - A[R])){
  28. cout<<A[L]<<'\n';
  29. }
  30. else cout<<A[R]<<'\n';
  31. }
  32.  
  33.  
  34. int main()
  35. {
  36. int n,k;
  37. cin>>n>>k;
  38. A.resize(n);
  39. for (int &x: A) cin>>x;
  40. vector <int> B(k);
  41. for (int &x: B) cin>>x;
  42. for (int x: B){
  43. bp(x);
  44. }
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement