Advertisement
inhuman_Arif

Playboy chimp V2

Jan 24th, 2021
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. typedef long long ll;
  5.  
  6. ll lowerBound(ll arr[],ll n,ll x)
  7. {
  8.     ll l=0 , r=n-1;
  9.  
  10.     ll ans=-1;
  11.     while(l<=r)
  12.     {
  13.         ll mid = l+(r-l)/2;
  14.         if(arr[mid]>=x)
  15.         {
  16.             ans=mid;
  17.             r=mid-1;
  18.         }
  19.         else
  20.             l=mid+1;
  21.     }
  22.     return ans;
  23. }
  24. ll upperBound(ll arr[],ll n,ll x)
  25. {
  26.     ll l=0,r=n-1;
  27.  
  28.     ll ans=-1;
  29.     while(l<=r)
  30.     {
  31.         ll mid = l+(r-l)/2;
  32.         if(arr[mid]<=x)
  33.         {
  34.             ans=mid;
  35.             l=mid+1;
  36.         }
  37.         else
  38.             r=mid-1;
  39.     }
  40.     return ans+1;
  41. }
  42.  
  43. int main()
  44. {
  45.     ll ch,query;
  46.     cin >> ch;
  47.     ll chimp[ch];
  48.     for(int i=0;i<ch;i++)
  49.         cin >> chimp[i];
  50.     cin >> query;
  51.     ll luchu[query];
  52.     for(int i=0;i<query;i++)
  53.         cin >> luchu[i];
  54.     ll s,t;
  55.     for(ll i=0;i<query;i++)
  56.     {
  57.         s = lowerBound(chimp,ch,luchu[i]);
  58.         t = upperBound(chimp,ch,luchu[i]);
  59.         if(s==0)
  60.             cout << "X ";
  61.         else if(s==-1)
  62.             cout << chimp[ch-1] << " ";
  63.         else
  64.             cout << chimp[s-1] << " ";
  65.         if(t==ch)
  66.             cout << "X" << endl;
  67.         else
  68.             cout << chimp[t] << endl;
  69.     }
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement