Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int upper(int a[],int n,int t)
- {
- int m,l=0,h=n-1;
- while(l<=h)
- {
- m=(l+h)/2;
- if(a[m]==t)
- {
- l=m+1;
- }
- else if(a[m]<t)l=m+1;
- else h=m-1;
- }
- return m;
- }
- int lower(int a[],int n,int t)
- {
- int m,l=0,h=n-1;
- while(l<=h)
- {
- m=(l+h)/2;
- if(a[m]==t)
- {
- h=m-1;
- }
- else if(a[m]<t)l=m+1;
- else h=m-1;
- }
- return m;
- }
- int main()
- {
- freopen("In.txt","r",stdin);
- freopen("Out.txt","w",stdout);
- int n,t,q,q1;
- cin>>n;
- int a[n];
- for(int i=0; i<n; i++)
- cin>>a[i];
- cin>>q;
- for(int i=0; i<q; i++)
- {
- cin>>q1;
- int y=lower(a,n,q1);
- if(a[y]<q1)cout<<a[y]<<" ";
- else if(a[y-1]<q1)cout<<a[y-1]<<" ";
- else cout<<"X ";
- int x=upper(a,n,q1);
- if(a[x]>q1)cout<<a[x]<<endl;
- else if(a[x+1]>q1)cout<<a[x+1]<<endl;
- else cout<<" X"<<endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement