#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 |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
600 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 |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
416 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 |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
600 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 |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
416 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 |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
15 ms |
1884 KB |
Output is correct |
18 |
Correct |
15 ms |
1884 KB |
Output is correct |
19 |
Correct |
14 ms |
1884 KB |
Output is correct |
20 |
Correct |
14 ms |
1884 KB |
Output is correct |
21 |
Correct |
15 ms |
1880 KB |
Output is correct |
22 |
Correct |
15 ms |
1884 KB |
Output is correct |
23 |
Correct |
16 ms |
1884 KB |
Output is correct |
24 |
Correct |
14 ms |
1884 KB |
Output is correct |
25 |
Correct |
15 ms |
1884 KB |
Output is correct |
26 |
Correct |
14 ms |
2136 KB |
Output is correct |
27 |
Correct |
15 ms |
1884 KB |
Output is correct |
28 |
Correct |
14 ms |
1884 KB |
Output is correct |
29 |
Correct |
15 ms |
1884 KB |
Output is correct |
30 |
Correct |
14 ms |
1908 KB |
Output is correct |
31 |
Correct |
14 ms |
1884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
348 ms |
3232 KB |
Output is correct |
8 |
Correct |
347 ms |
3400 KB |
Output is correct |
9 |
Correct |
346 ms |
3232 KB |
Output is correct |
10 |
Correct |
346 ms |
3620 KB |
Output is correct |
11 |
Correct |
343 ms |
3332 KB |
Output is correct |
12 |
Correct |
348 ms |
3452 KB |
Output is correct |
13 |
Correct |
30 ms |
2392 KB |
Output is correct |
14 |
Correct |
21 ms |
856 KB |
Output is correct |
15 |
Correct |
229 ms |
3948 KB |
Output is correct |
16 |
Correct |
232 ms |
3636 KB |
Output is correct |
17 |
Correct |
228 ms |
3588 KB |
Output is correct |
18 |
Correct |
31 ms |
2128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
600 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 |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
416 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 |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
15 ms |
1884 KB |
Output is correct |
18 |
Correct |
15 ms |
1884 KB |
Output is correct |
19 |
Correct |
14 ms |
1884 KB |
Output is correct |
20 |
Correct |
14 ms |
1884 KB |
Output is correct |
21 |
Correct |
15 ms |
1880 KB |
Output is correct |
22 |
Correct |
15 ms |
1884 KB |
Output is correct |
23 |
Correct |
16 ms |
1884 KB |
Output is correct |
24 |
Correct |
14 ms |
1884 KB |
Output is correct |
25 |
Correct |
15 ms |
1884 KB |
Output is correct |
26 |
Correct |
14 ms |
2136 KB |
Output is correct |
27 |
Correct |
15 ms |
1884 KB |
Output is correct |
28 |
Correct |
14 ms |
1884 KB |
Output is correct |
29 |
Correct |
15 ms |
1884 KB |
Output is correct |
30 |
Correct |
14 ms |
1908 KB |
Output is correct |
31 |
Correct |
14 ms |
1884 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
344 KB |
Output is correct |
35 |
Correct |
0 ms |
344 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
348 ms |
3232 KB |
Output is correct |
39 |
Correct |
347 ms |
3400 KB |
Output is correct |
40 |
Correct |
346 ms |
3232 KB |
Output is correct |
41 |
Correct |
346 ms |
3620 KB |
Output is correct |
42 |
Correct |
343 ms |
3332 KB |
Output is correct |
43 |
Correct |
348 ms |
3452 KB |
Output is correct |
44 |
Correct |
30 ms |
2392 KB |
Output is correct |
45 |
Correct |
21 ms |
856 KB |
Output is correct |
46 |
Correct |
229 ms |
3948 KB |
Output is correct |
47 |
Correct |
232 ms |
3636 KB |
Output is correct |
48 |
Correct |
228 ms |
3588 KB |
Output is correct |
49 |
Correct |
31 ms |
2128 KB |
Output is correct |
50 |
Correct |
312 ms |
4096 KB |
Output is correct |
51 |
Correct |
250 ms |
4436 KB |
Output is correct |
52 |
Correct |
239 ms |
4040 KB |
Output is correct |
53 |
Correct |
413 ms |
4080 KB |
Output is correct |
54 |
Correct |
407 ms |
4076 KB |
Output is correct |
55 |
Correct |
405 ms |
4076 KB |
Output is correct |
56 |
Correct |
55 ms |
3924 KB |
Output is correct |
57 |
Correct |
55 ms |
3924 KB |
Output is correct |
58 |
Correct |
55 ms |
3920 KB |
Output is correct |