#ifdef Home
#define _GLIBCXX_DEBUG
#endif // Home
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int N = 200200;
long long a[N];
main() {
#ifdef Home
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif // Home
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, q, s, l, r, m, cr_l, cr_r, ans;
cin >> n;
for(int i = 0; i < n; ++ i) {
cin >> a[i];
}
for(cin >> q; q --> 0;) {
cin >> s;
cr_l = -1, cr_r = n;
ans = 0;
for(; cr_l + 1 < cr_r;) {
m = (cr_l + cr_r) / 2;
(a[m] > s ? cr_r : cr_l) = m;
}
for(; cr_l != -1 && cr_r != n;) {
if(s - a[cr_l] > a[cr_r] - s) {
l = cr_r, r = n;
for(; l + 1 < r;) {
m = (l + r) / 2;
(a[m] - s < s - a[cr_l] ? l : r) = m;
}
ans += a[l] - s;
s = a[l];
cr_r = r;
} else {
l = -1, r = cr_l;
for(; l + 1 < r;) {
m = (l + r) / 2;
(a[cr_r] - s < s - a[l] ? l : r) = m;
}
ans += s - a[r];
s = a[r];
cr_l = l;
}
}
if(cr_r < n) {
ans += a[n - 1] - s;
} else if(cr_l > -1) {
ans += s - a[0];
}
cout << ans << '\n';
}
}
Compilation message
travel.cpp:16:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
16 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
16 ms |
3712 KB |
Output is correct |
18 |
Correct |
18 ms |
3792 KB |
Output is correct |
19 |
Correct |
16 ms |
3740 KB |
Output is correct |
20 |
Correct |
16 ms |
3796 KB |
Output is correct |
21 |
Correct |
18 ms |
3744 KB |
Output is correct |
22 |
Correct |
20 ms |
3804 KB |
Output is correct |
23 |
Correct |
19 ms |
3768 KB |
Output is correct |
24 |
Correct |
22 ms |
3772 KB |
Output is correct |
25 |
Correct |
20 ms |
3788 KB |
Output is correct |
26 |
Correct |
20 ms |
3748 KB |
Output is correct |
27 |
Correct |
17 ms |
3696 KB |
Output is correct |
28 |
Correct |
21 ms |
3724 KB |
Output is correct |
29 |
Correct |
18 ms |
3660 KB |
Output is correct |
30 |
Correct |
19 ms |
3732 KB |
Output is correct |
31 |
Correct |
20 ms |
3724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
160 ms |
5708 KB |
Output is correct |
8 |
Correct |
147 ms |
5692 KB |
Output is correct |
9 |
Correct |
152 ms |
5696 KB |
Output is correct |
10 |
Correct |
148 ms |
5604 KB |
Output is correct |
11 |
Correct |
164 ms |
5508 KB |
Output is correct |
12 |
Correct |
142 ms |
5504 KB |
Output is correct |
13 |
Correct |
32 ms |
4188 KB |
Output is correct |
14 |
Correct |
24 ms |
1488 KB |
Output is correct |
15 |
Correct |
173 ms |
6828 KB |
Output is correct |
16 |
Correct |
172 ms |
6812 KB |
Output is correct |
17 |
Correct |
170 ms |
6816 KB |
Output is correct |
18 |
Correct |
33 ms |
4116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
16 ms |
3712 KB |
Output is correct |
18 |
Correct |
18 ms |
3792 KB |
Output is correct |
19 |
Correct |
16 ms |
3740 KB |
Output is correct |
20 |
Correct |
16 ms |
3796 KB |
Output is correct |
21 |
Correct |
18 ms |
3744 KB |
Output is correct |
22 |
Correct |
20 ms |
3804 KB |
Output is correct |
23 |
Correct |
19 ms |
3768 KB |
Output is correct |
24 |
Correct |
22 ms |
3772 KB |
Output is correct |
25 |
Correct |
20 ms |
3788 KB |
Output is correct |
26 |
Correct |
20 ms |
3748 KB |
Output is correct |
27 |
Correct |
17 ms |
3696 KB |
Output is correct |
28 |
Correct |
21 ms |
3724 KB |
Output is correct |
29 |
Correct |
18 ms |
3660 KB |
Output is correct |
30 |
Correct |
19 ms |
3732 KB |
Output is correct |
31 |
Correct |
20 ms |
3724 KB |
Output is correct |
32 |
Correct |
1 ms |
328 KB |
Output is correct |
33 |
Correct |
1 ms |
324 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
160 ms |
5708 KB |
Output is correct |
39 |
Correct |
147 ms |
5692 KB |
Output is correct |
40 |
Correct |
152 ms |
5696 KB |
Output is correct |
41 |
Correct |
148 ms |
5604 KB |
Output is correct |
42 |
Correct |
164 ms |
5508 KB |
Output is correct |
43 |
Correct |
142 ms |
5504 KB |
Output is correct |
44 |
Correct |
32 ms |
4188 KB |
Output is correct |
45 |
Correct |
24 ms |
1488 KB |
Output is correct |
46 |
Correct |
173 ms |
6828 KB |
Output is correct |
47 |
Correct |
172 ms |
6812 KB |
Output is correct |
48 |
Correct |
170 ms |
6816 KB |
Output is correct |
49 |
Correct |
33 ms |
4116 KB |
Output is correct |
50 |
Correct |
293 ms |
7932 KB |
Output is correct |
51 |
Correct |
253 ms |
7852 KB |
Output is correct |
52 |
Correct |
234 ms |
7992 KB |
Output is correct |
53 |
Correct |
302 ms |
7932 KB |
Output is correct |
54 |
Correct |
314 ms |
7904 KB |
Output is correct |
55 |
Correct |
289 ms |
7884 KB |
Output is correct |
56 |
Correct |
70 ms |
7740 KB |
Output is correct |
57 |
Correct |
75 ms |
7700 KB |
Output is correct |
58 |
Correct |
78 ms |
7748 KB |
Output is correct |