#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
468 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 |
388 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 |
452 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 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 |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
468 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 |
388 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 |
452 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
15 ms |
3940 KB |
Output is correct |
18 |
Correct |
15 ms |
3920 KB |
Output is correct |
19 |
Correct |
15 ms |
3932 KB |
Output is correct |
20 |
Correct |
15 ms |
3816 KB |
Output is correct |
21 |
Correct |
15 ms |
3932 KB |
Output is correct |
22 |
Correct |
16 ms |
3932 KB |
Output is correct |
23 |
Correct |
16 ms |
3932 KB |
Output is correct |
24 |
Correct |
15 ms |
3932 KB |
Output is correct |
25 |
Correct |
15 ms |
3932 KB |
Output is correct |
26 |
Correct |
15 ms |
3932 KB |
Output is correct |
27 |
Correct |
16 ms |
3928 KB |
Output is correct |
28 |
Correct |
16 ms |
3932 KB |
Output is correct |
29 |
Correct |
15 ms |
3812 KB |
Output is correct |
30 |
Correct |
15 ms |
3688 KB |
Output is correct |
31 |
Correct |
15 ms |
3840 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 |
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 |
340 ms |
3308 KB |
Output is correct |
8 |
Correct |
352 ms |
6164 KB |
Output is correct |
9 |
Correct |
339 ms |
5708 KB |
Output is correct |
10 |
Correct |
342 ms |
5904 KB |
Output is correct |
11 |
Correct |
348 ms |
5724 KB |
Output is correct |
12 |
Correct |
338 ms |
5912 KB |
Output is correct |
13 |
Correct |
32 ms |
4180 KB |
Output is correct |
14 |
Correct |
23 ms |
1620 KB |
Output is correct |
15 |
Correct |
227 ms |
6928 KB |
Output is correct |
16 |
Correct |
227 ms |
6920 KB |
Output is correct |
17 |
Correct |
227 ms |
6984 KB |
Output is correct |
18 |
Correct |
34 ms |
4072 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 |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
468 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 |
388 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 |
452 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
15 ms |
3940 KB |
Output is correct |
18 |
Correct |
15 ms |
3920 KB |
Output is correct |
19 |
Correct |
15 ms |
3932 KB |
Output is correct |
20 |
Correct |
15 ms |
3816 KB |
Output is correct |
21 |
Correct |
15 ms |
3932 KB |
Output is correct |
22 |
Correct |
16 ms |
3932 KB |
Output is correct |
23 |
Correct |
16 ms |
3932 KB |
Output is correct |
24 |
Correct |
15 ms |
3932 KB |
Output is correct |
25 |
Correct |
15 ms |
3932 KB |
Output is correct |
26 |
Correct |
15 ms |
3932 KB |
Output is correct |
27 |
Correct |
16 ms |
3928 KB |
Output is correct |
28 |
Correct |
16 ms |
3932 KB |
Output is correct |
29 |
Correct |
15 ms |
3812 KB |
Output is correct |
30 |
Correct |
15 ms |
3688 KB |
Output is correct |
31 |
Correct |
15 ms |
3840 KB |
Output is correct |
32 |
Correct |
0 ms |
344 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 |
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 |
340 ms |
3308 KB |
Output is correct |
39 |
Correct |
352 ms |
6164 KB |
Output is correct |
40 |
Correct |
339 ms |
5708 KB |
Output is correct |
41 |
Correct |
342 ms |
5904 KB |
Output is correct |
42 |
Correct |
348 ms |
5724 KB |
Output is correct |
43 |
Correct |
338 ms |
5912 KB |
Output is correct |
44 |
Correct |
32 ms |
4180 KB |
Output is correct |
45 |
Correct |
23 ms |
1620 KB |
Output is correct |
46 |
Correct |
227 ms |
6928 KB |
Output is correct |
47 |
Correct |
227 ms |
6920 KB |
Output is correct |
48 |
Correct |
227 ms |
6984 KB |
Output is correct |
49 |
Correct |
34 ms |
4072 KB |
Output is correct |
50 |
Correct |
327 ms |
7888 KB |
Output is correct |
51 |
Correct |
253 ms |
8084 KB |
Output is correct |
52 |
Correct |
237 ms |
7872 KB |
Output is correct |
53 |
Correct |
412 ms |
8056 KB |
Output is correct |
54 |
Correct |
398 ms |
7896 KB |
Output is correct |
55 |
Correct |
426 ms |
8236 KB |
Output is correct |
56 |
Correct |
56 ms |
7908 KB |
Output is correct |
57 |
Correct |
56 ms |
7804 KB |
Output is correct |
58 |
Correct |
58 ms |
8036 KB |
Output is correct |