-
Total time limit:
-
1000ms
-
Memory Limit:
-
65536kB
-
Describe
-
In a non-descending sequence, finds the element closest to the given value.
-
Input
-
The first line contains an integer n, which is a non-descending sequence length. 1 <= n <= 100000.
The second line contains n integers, which are non-descending sequence elements. The size of all elements is between 0-1 and 000,000,000.
The third line contains an integer m, which is the number of the given value to ask. 1 <= m <= 10000.
Next m line, one integer per line, is the given value to ask the closest element. The size of all the given values is between 0-1 and 000,000,000.
-
Output
-
m rows, one integer per line, for the element value closest to the corresponding given value, preserving the input order. If more than one value satisfies the condition, the output is the smallest one.
-
Sample input
-
32 5 82105
-
Sample output
-
85
1#include <stdio.h>2#include <stdlib.h>3 #defineMAXN 1000104 intA[MAXN];5 intN;6 intcmpConst void*a,Const void*b)7 {8 return*(int*) a-* (int*) b;9 }Ten voidSearchintx) One { A intmid,l,r,ans=-1; -mid=n/2; -L=0; theR=N; - while(l<r-1) - { - if(a[mid]>x) r=mid; + ElseL=mid; -Mid= (r+l)/2; + } A if(L +1==n| | x-a[l]<=a[l+1]-x) ans=A[l]; at Elseans=a[l+1]; -printf"%d\n", ans); - } - intMain () - { - intm,f; in inti; -scanf"%d",&n); to for(i=0; i<n;i++) +scanf"%d",&a[i]); -Qsort (A,n,sizeof(int), CMP); thescanf"%d",&m); * while(m--) $ {Panax Notoginsengscanf"%d",&f); - search (f); the } + return 0; A}
As a basic two-part question, it seems that there is nothing to talk about, that is, while (m--) little tricks.
Openjudge Noi question Bank ch0111/01 find the nearest element