1 | #include <bits/stdc++.h> |
2 | |
3 | using namespace std; |
4 | |
5 | int main() { |
6 | std::ios_base::sync_with_stdio(false); |
7 | cin.tie(0); |
8 | cout.tie(0); |
9 | int n, k; |
10 | cin >> n >> k; |
11 | int a[n]; |
12 | for (int i = 0; i < n; ++i) { |
13 | cin >> a[i]; |
14 | } |
15 | |
16 | for (int i = 0; i < k; ++i) { |
17 | int x; |
18 | cin >> x; |
19 | |
20 | int j = (lower_bound(a + 0, a + n, x) - a); |
21 | |
22 | if (j == n) { |
23 | cout << a[n - 1] << endl; |
24 | } else if (j == 0) { |
25 | cout << a[0] << endl; |
26 | } else if (x - a[j - 1] <= a[j] - x) { |
27 | cout << a[j - 1] << endl; |
28 | } else { |
29 | cout << a[j] << endl; |
30 | } |
31 | } |
32 | |
33 | return 0; |
34 | } |
35 | |
36 | |
37 | 5 |
38 | 0 1 2 3 4 |
39 | 1 3 5 8 9 |
40 | 5 |
41 | 2 |
42 | 7 |
43 | 9 |
44 | 100 |
45 | 1 |
46 | a[j - 1] < x <= a[j] |
47 | x <= a[0] |
48 | |
49 | a[0] < 2 <= a[1] |
50 | a[2] < 7 <= a[3] |
51 | a[3] < 9 <= a[4] |
52 | */ |
53 | |
54 | |
55 | |