#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include<bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define pb push_back
#define ii pair<int, int>
#define sz(v) (int)v.size()
#define all(v) v.begin(), v.end()
using namespace std;
const int N=2e5+5, mod = 1e9+7, inf = 1e18;
int n, q;
int a[N];
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for (int i=1; i<=n; i++) cin >> a[i];
cin >> q;
while (q--) {
int s; cin >> s;
int it = lower_bound(a+1, a+n+1, s)-a;
int l, r;
if (it==n+1) l=r=n;
else if (a[it]==s || it==1) l=r=it;
else if (a[it]-s<s-a[it - 1]) l=r=it;
else l=r=it-1;
int res = abs(s-a[l]);
s = a[l];
while(1) {
if(l==1) {
res += abs(s - a[n]);
break;
}
if (r==n) {
res += abs(s - a[1]);
break;
}
int L=a[l-1], R=a[r+1];
if (s-L<=R-s) {
int nxt = lower_bound(a+1, a+n+1, 2*s-R)-a;
res += abs(s-a[nxt]);
s = a[nxt];
l = nxt;
} else {
int nxt = upper_bound(a+1, a+n+1, 2*s-L)-a-1;
res += abs(s-a[nxt]);
s = a[nxt];
r = nxt;
}
}
cout << res << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
484 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
500 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
592 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
484 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
500 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
592 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
336 KB |
Output is correct |
17 |
Correct |
17 ms |
3756 KB |
Output is correct |
18 |
Correct |
17 ms |
3912 KB |
Output is correct |
19 |
Correct |
17 ms |
3920 KB |
Output is correct |
20 |
Correct |
17 ms |
3800 KB |
Output is correct |
21 |
Correct |
17 ms |
3920 KB |
Output is correct |
22 |
Correct |
17 ms |
3920 KB |
Output is correct |
23 |
Correct |
19 ms |
3932 KB |
Output is correct |
24 |
Correct |
17 ms |
4036 KB |
Output is correct |
25 |
Correct |
17 ms |
3920 KB |
Output is correct |
26 |
Correct |
16 ms |
3920 KB |
Output is correct |
27 |
Correct |
23 ms |
3804 KB |
Output is correct |
28 |
Correct |
17 ms |
3920 KB |
Output is correct |
29 |
Correct |
16 ms |
3664 KB |
Output is correct |
30 |
Correct |
21 ms |
3664 KB |
Output is correct |
31 |
Correct |
17 ms |
3672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
330 ms |
5840 KB |
Output is correct |
8 |
Correct |
334 ms |
5948 KB |
Output is correct |
9 |
Correct |
325 ms |
5868 KB |
Output is correct |
10 |
Correct |
320 ms |
5748 KB |
Output is correct |
11 |
Correct |
325 ms |
5892 KB |
Output is correct |
12 |
Correct |
324 ms |
5960 KB |
Output is correct |
13 |
Correct |
31 ms |
4168 KB |
Output is correct |
14 |
Correct |
28 ms |
1608 KB |
Output is correct |
15 |
Correct |
226 ms |
7024 KB |
Output is correct |
16 |
Correct |
223 ms |
6900 KB |
Output is correct |
17 |
Correct |
232 ms |
6984 KB |
Output is correct |
18 |
Correct |
30 ms |
4180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
484 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
500 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
592 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
336 KB |
Output is correct |
17 |
Correct |
17 ms |
3756 KB |
Output is correct |
18 |
Correct |
17 ms |
3912 KB |
Output is correct |
19 |
Correct |
17 ms |
3920 KB |
Output is correct |
20 |
Correct |
17 ms |
3800 KB |
Output is correct |
21 |
Correct |
17 ms |
3920 KB |
Output is correct |
22 |
Correct |
17 ms |
3920 KB |
Output is correct |
23 |
Correct |
19 ms |
3932 KB |
Output is correct |
24 |
Correct |
17 ms |
4036 KB |
Output is correct |
25 |
Correct |
17 ms |
3920 KB |
Output is correct |
26 |
Correct |
16 ms |
3920 KB |
Output is correct |
27 |
Correct |
23 ms |
3804 KB |
Output is correct |
28 |
Correct |
17 ms |
3920 KB |
Output is correct |
29 |
Correct |
16 ms |
3664 KB |
Output is correct |
30 |
Correct |
21 ms |
3664 KB |
Output is correct |
31 |
Correct |
17 ms |
3672 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
504 KB |
Output is correct |
36 |
Correct |
1 ms |
336 KB |
Output is correct |
37 |
Correct |
1 ms |
336 KB |
Output is correct |
38 |
Correct |
330 ms |
5840 KB |
Output is correct |
39 |
Correct |
334 ms |
5948 KB |
Output is correct |
40 |
Correct |
325 ms |
5868 KB |
Output is correct |
41 |
Correct |
320 ms |
5748 KB |
Output is correct |
42 |
Correct |
325 ms |
5892 KB |
Output is correct |
43 |
Correct |
324 ms |
5960 KB |
Output is correct |
44 |
Correct |
31 ms |
4168 KB |
Output is correct |
45 |
Correct |
28 ms |
1608 KB |
Output is correct |
46 |
Correct |
226 ms |
7024 KB |
Output is correct |
47 |
Correct |
223 ms |
6900 KB |
Output is correct |
48 |
Correct |
232 ms |
6984 KB |
Output is correct |
49 |
Correct |
30 ms |
4180 KB |
Output is correct |
50 |
Correct |
263 ms |
8004 KB |
Output is correct |
51 |
Correct |
226 ms |
8008 KB |
Output is correct |
52 |
Correct |
221 ms |
7928 KB |
Output is correct |
53 |
Correct |
395 ms |
8008 KB |
Output is correct |
54 |
Correct |
398 ms |
8008 KB |
Output is correct |
55 |
Correct |
423 ms |
7856 KB |
Output is correct |
56 |
Correct |
59 ms |
7752 KB |
Output is correct |
57 |
Correct |
97 ms |
7756 KB |
Output is correct |
58 |
Correct |
55 ms |
7752 KB |
Output is correct |