Advertisement
sajol222

bfb

Feb 22nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int upper(int a[],int n,int t)
  4. {
  5.     int m,l=0,h=n-1;
  6.     while(l<=h)
  7.     {
  8.         m=(l+h)/2;
  9.         if(a[m]==t)
  10.         {
  11.             l=m+1;
  12.         }
  13.  
  14.         else if(a[m]<t)l=m+1;
  15.         else h=m-1;
  16.     }
  17.     return m;
  18. }
  19.  
  20. int lower(int a[],int n,int t)
  21. {
  22.     int m,l=0,h=n-1;
  23.     while(l<=h)
  24.     {
  25.         m=(l+h)/2;
  26.         if(a[m]==t)
  27.         {
  28.             h=m-1;
  29.         }
  30.  
  31.         else if(a[m]<t)l=m+1;
  32.         else h=m-1;
  33.     }
  34.     return m;
  35. }
  36. int main()
  37. {
  38.    freopen("In.txt","r",stdin);
  39.     freopen("Out.txt","w",stdout);
  40.     int n,t,q,q1;
  41.     cin>>n;
  42.     int a[n];
  43.     for(int i=0; i<n; i++)
  44.         cin>>a[i];
  45.  
  46.         cin>>q;
  47.     for(int i=0; i<q; i++)
  48.     {
  49.         cin>>q1;
  50.  
  51.         int y=lower(a,n,q1);
  52.  
  53.  
  54.       if(a[y]<q1)cout<<a[y]<<" ";
  55.       else if(a[y-1]<q1)cout<<a[y-1]<<" ";
  56.       else cout<<"X ";
  57.  
  58.       int x=upper(a,n,q1);
  59.  
  60.       if(a[x]>q1)cout<<a[x]<<endl;
  61.       else if(a[x+1]>q1)cout<<a[x+1]<<endl;
  62.       else cout<<" X"<<endl;
  63.  
  64.  
  65.  
  66.  
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement