#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 |
2 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
560 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 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 |
1 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
2 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
560 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 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 |
1 ms |
504 KB |
Output is correct |
17 |
Correct |
24 ms |
3920 KB |
Output is correct |
18 |
Correct |
19 ms |
3664 KB |
Output is correct |
19 |
Correct |
18 ms |
3152 KB |
Output is correct |
20 |
Correct |
18 ms |
3144 KB |
Output is correct |
21 |
Correct |
20 ms |
3920 KB |
Output is correct |
22 |
Correct |
18 ms |
3676 KB |
Output is correct |
23 |
Correct |
20 ms |
3688 KB |
Output is correct |
24 |
Correct |
24 ms |
3744 KB |
Output is correct |
25 |
Correct |
17 ms |
3932 KB |
Output is correct |
26 |
Correct |
26 ms |
3664 KB |
Output is correct |
27 |
Correct |
17 ms |
3152 KB |
Output is correct |
28 |
Correct |
23 ms |
3060 KB |
Output is correct |
29 |
Correct |
17 ms |
3724 KB |
Output is correct |
30 |
Correct |
16 ms |
3708 KB |
Output is correct |
31 |
Correct |
18 ms |
3152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
476 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 |
588 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
332 ms |
5624 KB |
Output is correct |
8 |
Correct |
342 ms |
4936 KB |
Output is correct |
9 |
Correct |
357 ms |
5704 KB |
Output is correct |
10 |
Correct |
348 ms |
4936 KB |
Output is correct |
11 |
Correct |
339 ms |
5532 KB |
Output is correct |
12 |
Correct |
330 ms |
5960 KB |
Output is correct |
13 |
Correct |
36 ms |
4036 KB |
Output is correct |
14 |
Correct |
21 ms |
1360 KB |
Output is correct |
15 |
Correct |
228 ms |
6480 KB |
Output is correct |
16 |
Correct |
231 ms |
5448 KB |
Output is correct |
17 |
Correct |
244 ms |
5448 KB |
Output is correct |
18 |
Correct |
31 ms |
3468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
2 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
560 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 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 |
1 ms |
504 KB |
Output is correct |
17 |
Correct |
24 ms |
3920 KB |
Output is correct |
18 |
Correct |
19 ms |
3664 KB |
Output is correct |
19 |
Correct |
18 ms |
3152 KB |
Output is correct |
20 |
Correct |
18 ms |
3144 KB |
Output is correct |
21 |
Correct |
20 ms |
3920 KB |
Output is correct |
22 |
Correct |
18 ms |
3676 KB |
Output is correct |
23 |
Correct |
20 ms |
3688 KB |
Output is correct |
24 |
Correct |
24 ms |
3744 KB |
Output is correct |
25 |
Correct |
17 ms |
3932 KB |
Output is correct |
26 |
Correct |
26 ms |
3664 KB |
Output is correct |
27 |
Correct |
17 ms |
3152 KB |
Output is correct |
28 |
Correct |
23 ms |
3060 KB |
Output is correct |
29 |
Correct |
17 ms |
3724 KB |
Output is correct |
30 |
Correct |
16 ms |
3708 KB |
Output is correct |
31 |
Correct |
18 ms |
3152 KB |
Output is correct |
32 |
Correct |
1 ms |
476 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 |
588 KB |
Output is correct |
36 |
Correct |
1 ms |
336 KB |
Output is correct |
37 |
Correct |
1 ms |
336 KB |
Output is correct |
38 |
Correct |
332 ms |
5624 KB |
Output is correct |
39 |
Correct |
342 ms |
4936 KB |
Output is correct |
40 |
Correct |
357 ms |
5704 KB |
Output is correct |
41 |
Correct |
348 ms |
4936 KB |
Output is correct |
42 |
Correct |
339 ms |
5532 KB |
Output is correct |
43 |
Correct |
330 ms |
5960 KB |
Output is correct |
44 |
Correct |
36 ms |
4036 KB |
Output is correct |
45 |
Correct |
21 ms |
1360 KB |
Output is correct |
46 |
Correct |
228 ms |
6480 KB |
Output is correct |
47 |
Correct |
231 ms |
5448 KB |
Output is correct |
48 |
Correct |
244 ms |
5448 KB |
Output is correct |
49 |
Correct |
31 ms |
3468 KB |
Output is correct |
50 |
Correct |
278 ms |
6608 KB |
Output is correct |
51 |
Correct |
234 ms |
6344 KB |
Output is correct |
52 |
Correct |
246 ms |
6456 KB |
Output is correct |
53 |
Correct |
410 ms |
7344 KB |
Output is correct |
54 |
Correct |
405 ms |
7520 KB |
Output is correct |
55 |
Correct |
430 ms |
6468 KB |
Output is correct |
56 |
Correct |
61 ms |
7240 KB |
Output is correct |
57 |
Correct |
60 ms |
5964 KB |
Output is correct |
58 |
Correct |
62 ms |
7196 KB |
Output is correct |