MartinPaunov

Lower_Upper_Bound

May 15th, 2018
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. #include <algorithm>
  4.  
  5.  
  6. int main() {
  7.  
  8.     std::cin.sync_with_stdio(false);
  9.     std::cout.sync_with_stdio(false);
  10.  
  11.     int n;
  12.     std::cin >> n;
  13.     std::vector<int> v;
  14.     for (int i = 0; i < n; i++) {
  15.         int num;
  16.         std::cin >> num;
  17.         v.push_back(num);
  18.     }
  19.  
  20.     int n2;
  21.     std::cin >> n2;
  22.     for (int i = 0; i < n2; i++) {
  23.  
  24.         int number_to_search;
  25.         std::cin >> number_to_search;
  26.  
  27.             std::vector<int>::iterator it_low = std::lower_bound(v.begin(), v.end(), number_to_search);
  28.             std::vector<int>::iterator it_up = std::upper_bound(v.begin(), v.end(), number_to_search);
  29.  
  30.             if (std::binary_search(v.begin(), v.end(), number_to_search)) {
  31.                 std::cout << "Yes " << it_low - v.begin() + 1 << std::endl;
  32.             }
  33.             else {
  34.                 std::cout << "No " << it_up - v.begin() + 1 << std::endl;
  35.             }
  36.         }
  37.  
  38.     return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment