#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
#define size(x) (int)x.size()
template<class S, class T>
bool chmin(S &a, const T &b) {
return a > b && (a = b) == b;
}
template<class S, class T>
bool chmax(S &a, const T &b) {
return a < b && (a = b) == b;
}
const int inf = 1e9 + 7;
const int INF = 1e18 + 7;
const int mod = 998244353;
int n;
vector<int> x;
int solve(int l, int r, bool direction) {
int p = x[direction ? r : l];
if (l == 0) {
return x[n - 1] - p;
} else if (r == n - 1) {
return p - x[0];
} else if (p - x[l - 1] <= x[r + 1] - p) {
int q = lower_bound(all(x), 2 * p - x[r + 1]) - x.begin();
return p - x[q] + solve(q, r, false);
} else {
int q = upper_bound(all(x), 2 * p - x[l - 1]) - x.begin() - 1;
return x[q] - p + solve(l, q, true);
}
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n;
x.resize(n);
for (int i = 0; i < n; ++i) {
cin >> x[i];
}
int q;
cin >> q;
while (q--) {
int s;
cin >> s;
int p = upper_bound(all(x), s) - x.begin() - 1;
if (p == -1) {
cout << x[n - 1] - s << '\n';
} else if (p == n - 1) {
cout << s - x[0] << '\n';
} else if (s - x[p] <= x[p + 1] - s) {
cout << s - x[p] + solve(p, p, false) << '\n';
} else {
cout << x[p + 1] - s + solve(p + 1, p + 1, true) << '\n';
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 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 |
424 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 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 |
424 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
15 ms |
1884 KB |
Output is correct |
18 |
Correct |
18 ms |
1884 KB |
Output is correct |
19 |
Correct |
15 ms |
1884 KB |
Output is correct |
20 |
Correct |
18 ms |
1884 KB |
Output is correct |
21 |
Correct |
16 ms |
1884 KB |
Output is correct |
22 |
Correct |
17 ms |
1884 KB |
Output is correct |
23 |
Correct |
17 ms |
1884 KB |
Output is correct |
24 |
Correct |
14 ms |
1884 KB |
Output is correct |
25 |
Correct |
18 ms |
1884 KB |
Output is correct |
26 |
Correct |
14 ms |
1884 KB |
Output is correct |
27 |
Correct |
15 ms |
2136 KB |
Output is correct |
28 |
Correct |
15 ms |
1892 KB |
Output is correct |
29 |
Correct |
15 ms |
1884 KB |
Output is correct |
30 |
Correct |
17 ms |
1884 KB |
Output is correct |
31 |
Correct |
15 ms |
1880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
347 ms |
3356 KB |
Output is correct |
8 |
Correct |
353 ms |
3324 KB |
Output is correct |
9 |
Correct |
348 ms |
3228 KB |
Output is correct |
10 |
Correct |
350 ms |
3220 KB |
Output is correct |
11 |
Correct |
345 ms |
3412 KB |
Output is correct |
12 |
Correct |
352 ms |
3428 KB |
Output is correct |
13 |
Correct |
31 ms |
2388 KB |
Output is correct |
14 |
Correct |
21 ms |
860 KB |
Output is correct |
15 |
Correct |
227 ms |
3632 KB |
Output is correct |
16 |
Correct |
228 ms |
3640 KB |
Output is correct |
17 |
Correct |
227 ms |
3592 KB |
Output is correct |
18 |
Correct |
31 ms |
2132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 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 |
424 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
15 ms |
1884 KB |
Output is correct |
18 |
Correct |
18 ms |
1884 KB |
Output is correct |
19 |
Correct |
15 ms |
1884 KB |
Output is correct |
20 |
Correct |
18 ms |
1884 KB |
Output is correct |
21 |
Correct |
16 ms |
1884 KB |
Output is correct |
22 |
Correct |
17 ms |
1884 KB |
Output is correct |
23 |
Correct |
17 ms |
1884 KB |
Output is correct |
24 |
Correct |
14 ms |
1884 KB |
Output is correct |
25 |
Correct |
18 ms |
1884 KB |
Output is correct |
26 |
Correct |
14 ms |
1884 KB |
Output is correct |
27 |
Correct |
15 ms |
2136 KB |
Output is correct |
28 |
Correct |
15 ms |
1892 KB |
Output is correct |
29 |
Correct |
15 ms |
1884 KB |
Output is correct |
30 |
Correct |
17 ms |
1884 KB |
Output is correct |
31 |
Correct |
15 ms |
1880 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
344 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 |
347 ms |
3356 KB |
Output is correct |
39 |
Correct |
353 ms |
3324 KB |
Output is correct |
40 |
Correct |
348 ms |
3228 KB |
Output is correct |
41 |
Correct |
350 ms |
3220 KB |
Output is correct |
42 |
Correct |
345 ms |
3412 KB |
Output is correct |
43 |
Correct |
352 ms |
3428 KB |
Output is correct |
44 |
Correct |
31 ms |
2388 KB |
Output is correct |
45 |
Correct |
21 ms |
860 KB |
Output is correct |
46 |
Correct |
227 ms |
3632 KB |
Output is correct |
47 |
Correct |
228 ms |
3640 KB |
Output is correct |
48 |
Correct |
227 ms |
3592 KB |
Output is correct |
49 |
Correct |
31 ms |
2132 KB |
Output is correct |
50 |
Correct |
310 ms |
3988 KB |
Output is correct |
51 |
Correct |
253 ms |
4132 KB |
Output is correct |
52 |
Correct |
243 ms |
4168 KB |
Output is correct |
53 |
Correct |
410 ms |
4168 KB |
Output is correct |
54 |
Correct |
406 ms |
4176 KB |
Output is correct |
55 |
Correct |
411 ms |
4432 KB |
Output is correct |
56 |
Correct |
56 ms |
3924 KB |
Output is correct |
57 |
Correct |
57 ms |
3920 KB |
Output is correct |
58 |
Correct |
56 ms |
3920 KB |
Output is correct |