#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 2e5 + 5;
int n, q;
int x[N];
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> x[i];
}
cin >> q;
while (q--) {
int s; cin >> s;
int it = lower_bound(x + 1, x + n + 1, s) - x;
int l, r;
if (it == n + 1) {
l = r = n;
} else if (x[it] == s || it == 1) {
l = r = it;
} else if (x[it] - s < s - x[it - 1]) {
l = r = it;
} else {
l = r = it - 1;
}
long long res = abs(s - x[l]);
s = x[l];
while (1) {
if (l == 1) {
res += abs(s - x[n]);
break;
}
if (r == n) {
res += abs(s - x[1]);
break;
}
int L = x[l - 1], R = x[r + 1];
if (s - L <= R - s) {
int nxt = lower_bound(x + 1, x + n + 1, 2 * s - R) - x;
res += abs(s - x[nxt]);
s = x[nxt];
l = nxt;
} else {
int nxt = upper_bound(x + 1, x + n + 1, 2 * s - L) - x - 1;
res += abs(s - x[nxt]);
s = x[nxt];
r = nxt;
}
}
cout << res << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
464 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
464 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
12 ms |
3164 KB |
Output is correct |
18 |
Correct |
14 ms |
2984 KB |
Output is correct |
19 |
Correct |
13 ms |
3164 KB |
Output is correct |
20 |
Correct |
12 ms |
3100 KB |
Output is correct |
21 |
Correct |
14 ms |
3080 KB |
Output is correct |
22 |
Correct |
16 ms |
3160 KB |
Output is correct |
23 |
Correct |
12 ms |
3164 KB |
Output is correct |
24 |
Correct |
12 ms |
3136 KB |
Output is correct |
25 |
Correct |
13 ms |
3164 KB |
Output is correct |
26 |
Correct |
14 ms |
3160 KB |
Output is correct |
27 |
Correct |
14 ms |
3164 KB |
Output is correct |
28 |
Correct |
15 ms |
3140 KB |
Output is correct |
29 |
Correct |
12 ms |
2908 KB |
Output is correct |
30 |
Correct |
13 ms |
2892 KB |
Output is correct |
31 |
Correct |
12 ms |
2880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
264 ms |
5152 KB |
Output is correct |
8 |
Correct |
257 ms |
4948 KB |
Output is correct |
9 |
Correct |
257 ms |
4936 KB |
Output is correct |
10 |
Correct |
254 ms |
4948 KB |
Output is correct |
11 |
Correct |
259 ms |
5164 KB |
Output is correct |
12 |
Correct |
253 ms |
4948 KB |
Output is correct |
13 |
Correct |
23 ms |
4180 KB |
Output is correct |
14 |
Correct |
22 ms |
1620 KB |
Output is correct |
15 |
Correct |
167 ms |
6228 KB |
Output is correct |
16 |
Correct |
172 ms |
6232 KB |
Output is correct |
17 |
Correct |
171 ms |
6228 KB |
Output is correct |
18 |
Correct |
23 ms |
4188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
464 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
12 ms |
3164 KB |
Output is correct |
18 |
Correct |
14 ms |
2984 KB |
Output is correct |
19 |
Correct |
13 ms |
3164 KB |
Output is correct |
20 |
Correct |
12 ms |
3100 KB |
Output is correct |
21 |
Correct |
14 ms |
3080 KB |
Output is correct |
22 |
Correct |
16 ms |
3160 KB |
Output is correct |
23 |
Correct |
12 ms |
3164 KB |
Output is correct |
24 |
Correct |
12 ms |
3136 KB |
Output is correct |
25 |
Correct |
13 ms |
3164 KB |
Output is correct |
26 |
Correct |
14 ms |
3160 KB |
Output is correct |
27 |
Correct |
14 ms |
3164 KB |
Output is correct |
28 |
Correct |
15 ms |
3140 KB |
Output is correct |
29 |
Correct |
12 ms |
2908 KB |
Output is correct |
30 |
Correct |
13 ms |
2892 KB |
Output is correct |
31 |
Correct |
12 ms |
2880 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
264 ms |
5152 KB |
Output is correct |
39 |
Correct |
257 ms |
4948 KB |
Output is correct |
40 |
Correct |
257 ms |
4936 KB |
Output is correct |
41 |
Correct |
254 ms |
4948 KB |
Output is correct |
42 |
Correct |
259 ms |
5164 KB |
Output is correct |
43 |
Correct |
253 ms |
4948 KB |
Output is correct |
44 |
Correct |
23 ms |
4180 KB |
Output is correct |
45 |
Correct |
22 ms |
1620 KB |
Output is correct |
46 |
Correct |
167 ms |
6228 KB |
Output is correct |
47 |
Correct |
172 ms |
6232 KB |
Output is correct |
48 |
Correct |
171 ms |
6228 KB |
Output is correct |
49 |
Correct |
23 ms |
4188 KB |
Output is correct |
50 |
Correct |
210 ms |
7252 KB |
Output is correct |
51 |
Correct |
175 ms |
7252 KB |
Output is correct |
52 |
Correct |
175 ms |
7248 KB |
Output is correct |
53 |
Correct |
301 ms |
7252 KB |
Output is correct |
54 |
Correct |
304 ms |
7276 KB |
Output is correct |
55 |
Correct |
300 ms |
7252 KB |
Output is correct |
56 |
Correct |
41 ms |
6992 KB |
Output is correct |
57 |
Correct |
40 ms |
6996 KB |
Output is correct |
58 |
Correct |
46 ms |
7152 KB |
Output is correct |