1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
#include <iostream> #include <cstdio> #include <cstring> using namespace std; int arr[50005]; int binSearch(int num,int lw,int hi) { int mid; while(lw<hi) { mid = (hi+lw)/2; if(num<arr[mid]) hi = mid; else if(num>=arr[mid]) lw = mid+1; } return hi; } int main() { int N,Q,a; int len,qry,idx; while(scanf("%d",&N)!=EOF) { scanf("%d",&arr[0]); len = 1; for(int i=1;i<N;i++) { scanf("%d",&a); if(a!=arr[len-1]) { arr[len]=a; len +=1; } } scanf("%d",&Q); for(int i=0;i<Q;i++) { scanf("%d",&qry); if(qry<arr[0]) { printf("X %d\n",arr[0]); } else if(qry==arr[0]) { printf("X"); if(len>1) printf(" %d\n",arr[1]); else printf("X\n"); } else if(qry==arr[len-1]) { printf("%d X\n",arr[len-2]); } else if(qry>arr[len-1]) { printf("%d X\n",arr[len-1]); } else { idx = binSearch(qry,0,len-1); if(arr[idx-1]==qry) { if(idx>=2) printf("%d %d\n",arr[idx-2],arr[idx]); else printf("X %d\n",arr[idx]); } else if(arr[idx-1]!=qry) printf("%d %d\n",arr[idx-1],arr[idx]); } } } return 0; } |