#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
//using namespace __gnu_pbds;
#define FOR(i, x, y) for(ll i=x; i<y; i++)
#define FORNEG(i, x, y) for(ll i=x; i>y; i--)
//#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define fast() ios_base::sync_with_stdio(false);cin.tie(NULL)
int main(){
ll n;
cin >> n;
vector<ll> pos(n);
FOR(i,0,n) cin >> pos[i];
ll q;
cin >> q;
FOR(i,0,q){
ll cur;
cin >> cur;
ll left = upper_bound(pos.begin(), pos.end(), cur) - pos.begin() - 1;
ll right = upper_bound(pos.begin(), pos.end(), cur) - pos.begin();
if (left < 0){
cout << abs(cur-pos[n-1]) << "\n";
continue;
}
if (right > n-1){
cout << abs(cur-pos[0]) << "\n";
continue;
}
ll ans = 0;
while (left>=0 && right<=n-1){
ll ldis = abs(cur-pos[left]);
ll rdis = abs(cur - pos[right]);
if (ldis > rdis){
ll maxpos = upper_bound(pos.begin(), pos.end(), cur+ldis-1) - pos.begin() - 1;
if (maxpos > n-1) maxpos = n-1;
ans += abs(pos[maxpos]-cur);
cur = pos[maxpos];
right = maxpos+1;
}else{
ll maxpos = lower_bound(pos.begin(), pos.end(), cur-rdis) - pos.begin();
if (maxpos < 0) maxpos = 0;
ans += abs(pos[maxpos]-cur);
cur = pos[maxpos];
left = maxpos-1;
}
}
if (left<0){
cout << ans + abs(cur-pos[n-1]) << "\n";
}else{
cout << ans + abs(cur-pos[0]) << "\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 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 |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
312 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 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 |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
312 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
57 ms |
3760 KB |
Output is correct |
18 |
Correct |
57 ms |
3756 KB |
Output is correct |
19 |
Correct |
56 ms |
3772 KB |
Output is correct |
20 |
Correct |
56 ms |
3784 KB |
Output is correct |
21 |
Correct |
57 ms |
3788 KB |
Output is correct |
22 |
Correct |
58 ms |
3756 KB |
Output is correct |
23 |
Correct |
64 ms |
3788 KB |
Output is correct |
24 |
Correct |
57 ms |
3764 KB |
Output is correct |
25 |
Correct |
56 ms |
3764 KB |
Output is correct |
26 |
Correct |
56 ms |
3760 KB |
Output is correct |
27 |
Correct |
56 ms |
3764 KB |
Output is correct |
28 |
Correct |
56 ms |
3756 KB |
Output is correct |
29 |
Correct |
62 ms |
3632 KB |
Output is correct |
30 |
Correct |
59 ms |
3628 KB |
Output is correct |
31 |
Correct |
56 ms |
3632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
625 ms |
3424 KB |
Output is correct |
8 |
Correct |
613 ms |
5784 KB |
Output is correct |
9 |
Correct |
619 ms |
5684 KB |
Output is correct |
10 |
Correct |
627 ms |
5760 KB |
Output is correct |
11 |
Correct |
629 ms |
5820 KB |
Output is correct |
12 |
Correct |
644 ms |
5768 KB |
Output is correct |
13 |
Correct |
277 ms |
4072 KB |
Output is correct |
14 |
Correct |
243 ms |
1484 KB |
Output is correct |
15 |
Correct |
520 ms |
6828 KB |
Output is correct |
16 |
Correct |
530 ms |
6836 KB |
Output is correct |
17 |
Correct |
514 ms |
6840 KB |
Output is correct |
18 |
Correct |
281 ms |
4108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 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 |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
312 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
57 ms |
3760 KB |
Output is correct |
18 |
Correct |
57 ms |
3756 KB |
Output is correct |
19 |
Correct |
56 ms |
3772 KB |
Output is correct |
20 |
Correct |
56 ms |
3784 KB |
Output is correct |
21 |
Correct |
57 ms |
3788 KB |
Output is correct |
22 |
Correct |
58 ms |
3756 KB |
Output is correct |
23 |
Correct |
64 ms |
3788 KB |
Output is correct |
24 |
Correct |
57 ms |
3764 KB |
Output is correct |
25 |
Correct |
56 ms |
3764 KB |
Output is correct |
26 |
Correct |
56 ms |
3760 KB |
Output is correct |
27 |
Correct |
56 ms |
3764 KB |
Output is correct |
28 |
Correct |
56 ms |
3756 KB |
Output is correct |
29 |
Correct |
62 ms |
3632 KB |
Output is correct |
30 |
Correct |
59 ms |
3628 KB |
Output is correct |
31 |
Correct |
56 ms |
3632 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
625 ms |
3424 KB |
Output is correct |
39 |
Correct |
613 ms |
5784 KB |
Output is correct |
40 |
Correct |
619 ms |
5684 KB |
Output is correct |
41 |
Correct |
627 ms |
5760 KB |
Output is correct |
42 |
Correct |
629 ms |
5820 KB |
Output is correct |
43 |
Correct |
644 ms |
5768 KB |
Output is correct |
44 |
Correct |
277 ms |
4072 KB |
Output is correct |
45 |
Correct |
243 ms |
1484 KB |
Output is correct |
46 |
Correct |
520 ms |
6828 KB |
Output is correct |
47 |
Correct |
530 ms |
6836 KB |
Output is correct |
48 |
Correct |
514 ms |
6840 KB |
Output is correct |
49 |
Correct |
281 ms |
4108 KB |
Output is correct |
50 |
Correct |
671 ms |
7928 KB |
Output is correct |
51 |
Correct |
565 ms |
7856 KB |
Output is correct |
52 |
Correct |
546 ms |
7864 KB |
Output is correct |
53 |
Correct |
722 ms |
7816 KB |
Output is correct |
54 |
Correct |
733 ms |
7860 KB |
Output is correct |
55 |
Correct |
726 ms |
7884 KB |
Output is correct |
56 |
Correct |
365 ms |
7712 KB |
Output is correct |
57 |
Correct |
357 ms |
7728 KB |
Output is correct |
58 |
Correct |
347 ms |
7780 KB |
Output is correct |