#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,a[1000000],q,st;
void solve(){
int ans=0;
auto* it=lower_bound(a+1,a+n+1,st);
int pos=it-a;
if(pos>0 && abs(st-a[pos-1])<=abs(a[pos]-st)){
pos--;
}
ans+=abs(st-a[pos]);
int x=pos-1;
int y=pos+1;
//cout<<pos<<endl;
while(1<=x || y<=n){
if(abs(a[x]-a[pos])<=abs(a[y]-a[pos])){
//cout<<"! goleft"<<endl;
int l=1;
int r=x;
while(l<=r){
int mid=(l+r)/2;
if(abs(a[pos]-a[mid])<=abs(a[y]-a[pos])){
r=mid-1;
}
else{
l=mid+1;
}
}
r++;
if(abs(a[r]-a[pos])<=abs(a[pos]-a[y])){
ans+=abs(a[r]-a[pos]);
x=r-1;
pos=r;
}
}
else{
//cout<<"! goright"<<endl;
int l=y;
int r=n;
while(l<=r){
int mid=(l+r)/2;
if(abs(a[mid]-a[pos])<abs(a[x]-a[pos])){
l=mid+1;
}
else{
r=mid-1;
}
}
l--;
if(abs(a[l]-a[pos])<abs(a[pos]-a[x])){
ans+=abs(a[pos]-a[l]);
pos=l;
y=l+1;
}
}
//cout<<x<<' '<<y<<' '<<pos<<endl;
}
cout<<ans<<endl;
}
signed main(){
cin>>n;
for(int i=1;n>=i;i++){
cin>>a[i];
}
a[0]=a[n+1]=100000000000000000;
cin>>q;
while(q--){
cin>>st;
solve();
}
}
# |
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 |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
312 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
11 |
Correct |
1 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 |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
316 KB |
Output is correct |
16 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
312 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
11 |
Correct |
1 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 |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
316 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
56 ms |
3776 KB |
Output is correct |
18 |
Correct |
64 ms |
3728 KB |
Output is correct |
19 |
Correct |
54 ms |
3736 KB |
Output is correct |
20 |
Correct |
55 ms |
3788 KB |
Output is correct |
21 |
Correct |
70 ms |
3704 KB |
Output is correct |
22 |
Correct |
55 ms |
3760 KB |
Output is correct |
23 |
Correct |
68 ms |
3724 KB |
Output is correct |
24 |
Correct |
69 ms |
3748 KB |
Output is correct |
25 |
Correct |
55 ms |
3744 KB |
Output is correct |
26 |
Correct |
54 ms |
3788 KB |
Output is correct |
27 |
Correct |
55 ms |
3756 KB |
Output is correct |
28 |
Correct |
56 ms |
3744 KB |
Output is correct |
29 |
Correct |
54 ms |
3712 KB |
Output is correct |
30 |
Correct |
54 ms |
3664 KB |
Output is correct |
31 |
Correct |
53 ms |
3644 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 |
1 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 |
591 ms |
3312 KB |
Output is correct |
8 |
Correct |
602 ms |
5716 KB |
Output is correct |
9 |
Correct |
578 ms |
5704 KB |
Output is correct |
10 |
Correct |
574 ms |
5928 KB |
Output is correct |
11 |
Correct |
565 ms |
5776 KB |
Output is correct |
12 |
Correct |
593 ms |
5720 KB |
Output is correct |
13 |
Correct |
278 ms |
4176 KB |
Output is correct |
14 |
Correct |
262 ms |
1460 KB |
Output is correct |
15 |
Correct |
505 ms |
6752 KB |
Output is correct |
16 |
Correct |
493 ms |
6852 KB |
Output is correct |
17 |
Correct |
578 ms |
6844 KB |
Output is correct |
18 |
Correct |
291 ms |
4204 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 |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
312 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
11 |
Correct |
1 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 |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
316 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
56 ms |
3776 KB |
Output is correct |
18 |
Correct |
64 ms |
3728 KB |
Output is correct |
19 |
Correct |
54 ms |
3736 KB |
Output is correct |
20 |
Correct |
55 ms |
3788 KB |
Output is correct |
21 |
Correct |
70 ms |
3704 KB |
Output is correct |
22 |
Correct |
55 ms |
3760 KB |
Output is correct |
23 |
Correct |
68 ms |
3724 KB |
Output is correct |
24 |
Correct |
69 ms |
3748 KB |
Output is correct |
25 |
Correct |
55 ms |
3744 KB |
Output is correct |
26 |
Correct |
54 ms |
3788 KB |
Output is correct |
27 |
Correct |
55 ms |
3756 KB |
Output is correct |
28 |
Correct |
56 ms |
3744 KB |
Output is correct |
29 |
Correct |
54 ms |
3712 KB |
Output is correct |
30 |
Correct |
54 ms |
3664 KB |
Output is correct |
31 |
Correct |
53 ms |
3644 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 |
1 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 |
591 ms |
3312 KB |
Output is correct |
39 |
Correct |
602 ms |
5716 KB |
Output is correct |
40 |
Correct |
578 ms |
5704 KB |
Output is correct |
41 |
Correct |
574 ms |
5928 KB |
Output is correct |
42 |
Correct |
565 ms |
5776 KB |
Output is correct |
43 |
Correct |
593 ms |
5720 KB |
Output is correct |
44 |
Correct |
278 ms |
4176 KB |
Output is correct |
45 |
Correct |
262 ms |
1460 KB |
Output is correct |
46 |
Correct |
505 ms |
6752 KB |
Output is correct |
47 |
Correct |
493 ms |
6852 KB |
Output is correct |
48 |
Correct |
578 ms |
6844 KB |
Output is correct |
49 |
Correct |
291 ms |
4204 KB |
Output is correct |
50 |
Correct |
671 ms |
7976 KB |
Output is correct |
51 |
Correct |
593 ms |
7884 KB |
Output is correct |
52 |
Correct |
545 ms |
7828 KB |
Output is correct |
53 |
Correct |
711 ms |
7920 KB |
Output is correct |
54 |
Correct |
778 ms |
7872 KB |
Output is correct |
55 |
Correct |
729 ms |
7972 KB |
Output is correct |
56 |
Correct |
349 ms |
7700 KB |
Output is correct |
57 |
Correct |
343 ms |
7736 KB |
Output is correct |
58 |
Correct |
347 ms |
7740 KB |
Output is correct |