D_L3

Намиране на елемент

Jan 25th, 2024
1,076
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8.  
  9. int main() {
  10.     int n, q, a;
  11.     cin >> n;
  12.     vector<int> arr(n);
  13.     for(int i = 0; i < n; i++)
  14.         cin >> arr[i];
  15.    
  16.     cin >> q;
  17.     for(int i = 0; i < q; i++){
  18.         cin >> a;
  19.         if(arr.empty()){
  20.             cout << 0 << endl;
  21.             continue;
  22.         }
  23.        
  24.         int lower = lower_bound(arr.begin(), arr.end(), a) - arr.begin();
  25.        
  26.         if(lower >= arr.size() || arr[lower] != a){
  27.             cout << lower << endl;
  28.             continue;
  29.         }
  30.         int upper = upper_bound(arr.begin(), arr.end(), a) - arr.begin() - 1;
  31.         cout << lower << " " << upper << endl;
  32.     }
  33.     return 0;
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment