#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
int n;
vector<int> a;
int solve(int l, int r, int x){
if(l <= 0){
return a[n-1] - x;
}else if(r >= n-1){
return x - a[0];
}else if(abs(a[l-1] - x) <= abs(a[r+1] - x)){
int q = lower_bound(all(a), x * 2 - a[r+1]) - a.begin();
return x - a[q] + solve(q, r, a[q]);
}else{
int q = upper_bound(all(a), x * 2 - a[l-1]) - a.begin() - 1;
return a[q] - x + solve(l, q, a[q]);
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
a.resize(n);
for(auto &e : a) cin >> e;
int q; cin >> q;
while(q--){
int x; cin >> x;
int pos = upper_bound(all(a), x) - a.begin() - 1;
if(pos < 0){
cout << abs(a.back() - x);
}else if(pos >= n-1){
cout << abs(x - a[0]);
}else if(abs(a[pos+1] - x) >= abs(a[pos] - x)){
cout << abs(a[pos] - x) + solve(pos, pos, a[pos]);
}else{
cout << abs(a[pos+1] - x) + solve(pos+1, pos+1, a[pos+1]);
}
cout << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
344 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 |
1 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 |
600 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
344 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 |
1 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 |
600 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 |
14 ms |
1884 KB |
Output is correct |
18 |
Correct |
14 ms |
1884 KB |
Output is correct |
19 |
Correct |
14 ms |
1884 KB |
Output is correct |
20 |
Correct |
15 ms |
1884 KB |
Output is correct |
21 |
Correct |
15 ms |
1884 KB |
Output is correct |
22 |
Correct |
15 ms |
1884 KB |
Output is correct |
23 |
Correct |
15 ms |
1884 KB |
Output is correct |
24 |
Correct |
15 ms |
1880 KB |
Output is correct |
25 |
Correct |
15 ms |
1884 KB |
Output is correct |
26 |
Correct |
17 ms |
1884 KB |
Output is correct |
27 |
Correct |
14 ms |
1884 KB |
Output is correct |
28 |
Correct |
14 ms |
1884 KB |
Output is correct |
29 |
Correct |
14 ms |
1880 KB |
Output is correct |
30 |
Correct |
14 ms |
1884 KB |
Output is correct |
31 |
Correct |
14 ms |
1884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
337 ms |
3412 KB |
Output is correct |
8 |
Correct |
339 ms |
3440 KB |
Output is correct |
9 |
Correct |
337 ms |
3408 KB |
Output is correct |
10 |
Correct |
349 ms |
3172 KB |
Output is correct |
11 |
Correct |
341 ms |
3256 KB |
Output is correct |
12 |
Correct |
345 ms |
3412 KB |
Output is correct |
13 |
Correct |
31 ms |
2384 KB |
Output is correct |
14 |
Correct |
23 ms |
860 KB |
Output is correct |
15 |
Correct |
225 ms |
3584 KB |
Output is correct |
16 |
Correct |
228 ms |
3668 KB |
Output is correct |
17 |
Correct |
224 ms |
3656 KB |
Output is correct |
18 |
Correct |
32 ms |
2132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
344 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 |
1 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 |
600 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 |
14 ms |
1884 KB |
Output is correct |
18 |
Correct |
14 ms |
1884 KB |
Output is correct |
19 |
Correct |
14 ms |
1884 KB |
Output is correct |
20 |
Correct |
15 ms |
1884 KB |
Output is correct |
21 |
Correct |
15 ms |
1884 KB |
Output is correct |
22 |
Correct |
15 ms |
1884 KB |
Output is correct |
23 |
Correct |
15 ms |
1884 KB |
Output is correct |
24 |
Correct |
15 ms |
1880 KB |
Output is correct |
25 |
Correct |
15 ms |
1884 KB |
Output is correct |
26 |
Correct |
17 ms |
1884 KB |
Output is correct |
27 |
Correct |
14 ms |
1884 KB |
Output is correct |
28 |
Correct |
14 ms |
1884 KB |
Output is correct |
29 |
Correct |
14 ms |
1880 KB |
Output is correct |
30 |
Correct |
14 ms |
1884 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 |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
344 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
337 ms |
3412 KB |
Output is correct |
39 |
Correct |
339 ms |
3440 KB |
Output is correct |
40 |
Correct |
337 ms |
3408 KB |
Output is correct |
41 |
Correct |
349 ms |
3172 KB |
Output is correct |
42 |
Correct |
341 ms |
3256 KB |
Output is correct |
43 |
Correct |
345 ms |
3412 KB |
Output is correct |
44 |
Correct |
31 ms |
2384 KB |
Output is correct |
45 |
Correct |
23 ms |
860 KB |
Output is correct |
46 |
Correct |
225 ms |
3584 KB |
Output is correct |
47 |
Correct |
228 ms |
3668 KB |
Output is correct |
48 |
Correct |
224 ms |
3656 KB |
Output is correct |
49 |
Correct |
32 ms |
2132 KB |
Output is correct |
50 |
Correct |
312 ms |
4084 KB |
Output is correct |
51 |
Correct |
246 ms |
4056 KB |
Output is correct |
52 |
Correct |
240 ms |
4468 KB |
Output is correct |
53 |
Correct |
406 ms |
4172 KB |
Output is correct |
54 |
Correct |
405 ms |
4112 KB |
Output is correct |
55 |
Correct |
398 ms |
4120 KB |
Output is correct |
56 |
Correct |
54 ms |
3924 KB |
Output is correct |
57 |
Correct |
61 ms |
3916 KB |
Output is correct |
58 |
Correct |
54 ms |
3920 KB |
Output is correct |