-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathw6_p2.cpp
More file actions
61 lines (46 loc) · 1.11 KB
/
Copy pathw6_p2.cpp
File metadata and controls
61 lines (46 loc) · 1.11 KB
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
#include <bits/stdc++.h>
using namespace std;
int main()
{
cin.tie(nullptr), ios::sync_with_stdio(false);
int N, Q;
cin >> N;
vector<int> point, query;
for (int i = 0; i < N; i++)
{
int dot;
cin >> dot;
point.push_back(dot);
}
sort(point.begin(), point.end());
cin >> Q;
for (int i = 0; i < Q; i++)
{
int q;
cin >> q;
int min = 0;
int max = point.size() - 1;
while (min != max)
{
int mid = (min + max + 1) / 2;
if (q >= point[mid])
min = mid;
else
max = mid - 1;
}
if (min == 0)
{
cout << point[min + 1] << endl;
continue;
}
else if (min == point.size() - 1)
{
cout << point[min - 1] << endl;
continue;
}
int dis_right = abs(point[min + 1] - point[min]);
int dis_left = abs(point[min] - point[min - 1]);
int most_min = dis_right < dis_left ? min + 1 : min - 1;
cout << point[most_min] << endl;
}
}