tungggg

lower_bound using recursion

Apr 9th, 2022 (edited)
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 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. #define ll long long
  9.  
  10. bool P(ll a, ll key ){
  11.      return a >= key ;
  12. }
  13.  
  14. int lower_b(ll * a, int last_index  , ll key , int l , int h ){
  15.     if ( l>=h ){
  16.         if ( P(a[l],key)== false ) return last_index+1;
  17.         return l;
  18.     }
  19.     else {
  20.         int mid = l+(h-l)/2;
  21.         if ( P(a[mid],key) ){
  22.             h=mid;
  23.         }
  24.         else {
  25.             l=mid+1;
  26.         }
  27.         return lower_b(a,last_index,key, l, h);
  28.     }
  29. }
  30.  
  31. int main() {
  32.     int n;
  33.     cin >> n;
  34.     ll * a= new  ll [n];
  35.     for (int i=0;i<n;i++) cin >> a[i];
  36.    
  37.     ll key;
  38.     cin >> key ;
  39.    
  40.     cout<<lower_b(a,n-1,key,0,n-1);
  41.    
  42.     /* Enter your code here. Read input from STDIN. Print output to STDOUT */  
  43.     return 0;
  44. }
  45.  
Add Comment
Please, Sign In to add comment