#ifdef Home
#define _GLIBCXX_DEBUG
#endif // Home
#include <bits/stdc++.h>
using namespace std;
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);
long long n, q, s;
cin >> n;
for(int i = 0; i < n; ++ i) {
cin >> a[i];
}
long long ans = 0, ll, rr, l, r, m, cr;
for(cin >> q; q --> 0;) {
cin >> s;
ll = -1, rr = n; // a[l] <= s < a[r];
for(; ll + 1 < rr;) {
m = (ll + rr) / 2;
(s < a[m] ? rr : ll) = m;
}
cr = (ll == -1 || (rr < n && a[rr] - s < s - a[ll]) ? rr : ll);
ans = abs(a[cr] - s);
cr == ll ? -- ll : ++ rr;
for(; ll > -1 && rr < n;) {
if(a[rr] - a[cr] < a[cr] - a[ll]) {
l = rr, r = n;
for(; l + 1 < r;) {
m = (l + r) / 2;
(a[m] - a[m - 1] < a[m - 1] - a[ll] ? l : r) = m;
}
ans += a[l] - a[cr];
cr = l;
rr = l + 1;
} else {
l = -1, r = ll;
for(; l + 1 < r;) {
m = (l + r) / 2;
(a[rr] - a[m + 1] < a[m + 1] - a[m] ? l : r) = m;
}
ans += a[cr] - a[r];
cr = r;
ll = r - 1;
}
}
if(ll < 0 && rr < n) {
ans += a[n - 1] - a[cr];
} else if(rr >= n && ll > -1) {
ans += a[cr] - a[0];
}
cout << ans << '\n';
}
}
Compilation message
travel.cpp:15:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
15 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
96 ms |
4028 KB |
Output is correct |
8 |
Correct |
121 ms |
4024 KB |
Output is correct |
9 |
Correct |
89 ms |
3980 KB |
Output is correct |
10 |
Correct |
86 ms |
4048 KB |
Output is correct |
11 |
Correct |
85 ms |
3940 KB |
Output is correct |
12 |
Correct |
88 ms |
4020 KB |
Output is correct |
13 |
Correct |
41 ms |
2996 KB |
Output is correct |
14 |
Correct |
25 ms |
1484 KB |
Output is correct |
15 |
Incorrect |
86 ms |
4428 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |