#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5;
int v[N];
int n;
int lb(int nr){
int l = 0,r = n;
while(l != r){
int mij = (l + r)/2;
if(v[mij] >= nr)r = mij;
else l = mij + 1;
}
return l;
}
int main(){
cin>>n;
for(int i = 0;i < n;i++){
cin>>v[i];
}
int q;
cin>>q;
for(int i = 0;i < q;i++){
int x;
cin>>x;
ll ans = 0;
int l,r,pos;
int tmp;
tmp = lb(x);
if(tmp != 0 && (tmp == n || x - v[tmp - 1] <= v[tmp] - x)){
ans+=x - v[tmp - 1];
l = tmp - 1;r = tmp - 1;
}else{
ans+=v[tmp] - x;
l = tmp;r = tmp;
}
pos = 0;
while(l != 0 || r != n - 1){
if(l == 0 || r == n - 1){
///go to n - 1
ans+=v[n - 1] - v[0];
l = 0;
r = n - 1;
continue;
}
if(pos == 0){
tmp = lb(v[l] - (v[r + 1] - v[l]));
if(tmp == l){
///bad go to r + 1
pos = 1;
ans+=v[r + 1] - v[l];
r++;
}else{
pos = 0;
ans+=v[l] - v[tmp];
l = tmp;
}
}else{
tmp = lb(v[r] + (v[r] - v[l - 1])) - 1;
if(tmp == r){
pos = 0;
ans+=v[r] - v[l - 1];
l--;
}else{
pos = 1;
ans+=v[tmp] - v[r];
r = tmp;
}
}
}
cout<<ans<<'\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
476 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 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 |
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 |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
472 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
476 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 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 |
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 |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
472 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
51 ms |
3132 KB |
Output is correct |
18 |
Correct |
46 ms |
2900 KB |
Output is correct |
19 |
Correct |
46 ms |
3112 KB |
Output is correct |
20 |
Correct |
46 ms |
3068 KB |
Output is correct |
21 |
Correct |
46 ms |
2896 KB |
Output is correct |
22 |
Correct |
85 ms |
3104 KB |
Output is correct |
23 |
Correct |
47 ms |
2900 KB |
Output is correct |
24 |
Correct |
49 ms |
2900 KB |
Output is correct |
25 |
Correct |
46 ms |
2900 KB |
Output is correct |
26 |
Correct |
47 ms |
2900 KB |
Output is correct |
27 |
Correct |
47 ms |
3136 KB |
Output is correct |
28 |
Correct |
54 ms |
2896 KB |
Output is correct |
29 |
Correct |
58 ms |
2936 KB |
Output is correct |
30 |
Correct |
49 ms |
2900 KB |
Output is correct |
31 |
Correct |
55 ms |
2892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
436 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
452 KB |
Output is correct |
4 |
Correct |
0 ms |
448 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
606 ms |
5056 KB |
Output is correct |
8 |
Correct |
583 ms |
4924 KB |
Output is correct |
9 |
Correct |
593 ms |
5144 KB |
Output is correct |
10 |
Correct |
559 ms |
4948 KB |
Output is correct |
11 |
Correct |
531 ms |
4984 KB |
Output is correct |
12 |
Correct |
541 ms |
4948 KB |
Output is correct |
13 |
Correct |
251 ms |
4180 KB |
Output is correct |
14 |
Correct |
208 ms |
1540 KB |
Output is correct |
15 |
Correct |
473 ms |
5972 KB |
Output is correct |
16 |
Correct |
434 ms |
5972 KB |
Output is correct |
17 |
Correct |
442 ms |
5972 KB |
Output is correct |
18 |
Correct |
241 ms |
4180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
476 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 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 |
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 |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
472 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
51 ms |
3132 KB |
Output is correct |
18 |
Correct |
46 ms |
2900 KB |
Output is correct |
19 |
Correct |
46 ms |
3112 KB |
Output is correct |
20 |
Correct |
46 ms |
3068 KB |
Output is correct |
21 |
Correct |
46 ms |
2896 KB |
Output is correct |
22 |
Correct |
85 ms |
3104 KB |
Output is correct |
23 |
Correct |
47 ms |
2900 KB |
Output is correct |
24 |
Correct |
49 ms |
2900 KB |
Output is correct |
25 |
Correct |
46 ms |
2900 KB |
Output is correct |
26 |
Correct |
47 ms |
2900 KB |
Output is correct |
27 |
Correct |
47 ms |
3136 KB |
Output is correct |
28 |
Correct |
54 ms |
2896 KB |
Output is correct |
29 |
Correct |
58 ms |
2936 KB |
Output is correct |
30 |
Correct |
49 ms |
2900 KB |
Output is correct |
31 |
Correct |
55 ms |
2892 KB |
Output is correct |
32 |
Correct |
1 ms |
436 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
452 KB |
Output is correct |
35 |
Correct |
0 ms |
448 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
606 ms |
5056 KB |
Output is correct |
39 |
Correct |
583 ms |
4924 KB |
Output is correct |
40 |
Correct |
593 ms |
5144 KB |
Output is correct |
41 |
Correct |
559 ms |
4948 KB |
Output is correct |
42 |
Correct |
531 ms |
4984 KB |
Output is correct |
43 |
Correct |
541 ms |
4948 KB |
Output is correct |
44 |
Correct |
251 ms |
4180 KB |
Output is correct |
45 |
Correct |
208 ms |
1540 KB |
Output is correct |
46 |
Correct |
473 ms |
5972 KB |
Output is correct |
47 |
Correct |
434 ms |
5972 KB |
Output is correct |
48 |
Correct |
442 ms |
5972 KB |
Output is correct |
49 |
Correct |
241 ms |
4180 KB |
Output is correct |
50 |
Correct |
530 ms |
7252 KB |
Output is correct |
51 |
Correct |
466 ms |
7252 KB |
Output is correct |
52 |
Correct |
509 ms |
7172 KB |
Output is correct |
53 |
Correct |
623 ms |
7136 KB |
Output is correct |
54 |
Correct |
662 ms |
7252 KB |
Output is correct |
55 |
Correct |
672 ms |
7252 KB |
Output is correct |
56 |
Correct |
340 ms |
6996 KB |
Output is correct |
57 |
Correct |
317 ms |
6996 KB |
Output is correct |
58 |
Correct |
347 ms |
7040 KB |
Output is correct |