Advertisement
Guest User

lower_bound and upper_bound

a guest
May 15th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include<sstream>
  7. #include<string>
  8. #include<climits>
  9. using namespace std;
  10.  
  11.  
  12. int main() {
  13.     /* Enter your code here. Read input from STDIN. Print output to STDOUT */
  14.     cin.sync_with_stdio(false);
  15.     cout.sync_with_stdio(false);
  16.  
  17.     int n;
  18.     cin >> n;
  19.     vector<int> v;
  20.     int num, max_num = INT_MIN;
  21.     for (int i = 0; i < n; i++) {
  22.         cin >> num;
  23.         if (num > max_num) {
  24.             max_num = num;
  25.         }
  26.         v.push_back(num);
  27.     }
  28.  
  29.     int n2;
  30.     cin >> n2;
  31.     for (int i = 0; i < n2; i++) {
  32.  
  33.         int number_to_search;
  34.         cin >> number_to_search;
  35.  
  36.         if (number_to_search > max_num) {
  37.             cout << "No " << v.size() << endl;
  38.         }
  39.         else {
  40.             vector<int>::iterator it_low = lower_bound(v.begin(), v.end(), number_to_search);
  41.             vector<int>::iterator it_up = upper_bound(v.begin(), v.end(), number_to_search);
  42.  
  43.             if (find(v.begin(), v.end(), number_to_search) != v.end()) {
  44.                 cout << "Yes " << it_low - v.begin() + 1 << endl;
  45.             }
  46.             else {
  47.                 cout << "No " << it_up - v.begin() + 1 << endl;
  48.             }
  49.         }
  50.     }
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement