#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define AquA cin.tie(0);ios_base::sync_with_stdio(0);
#define fs first
#define sc second
#define p_q priority_queue
#define int long long
using namespace std;
vector<int> e,f;
signed main(){
AquA;
int n;
cin >> n;
vector<int> v(n+2);
for(int i=1;i<=n;i++){
cin >> v[i];
}
const int inf=1e18;
v[0]=-inf;
v[n+1]=inf;
int q;
cin >> q;
while(q--){
int c;
cin >> c;
int p=lower_bound(v.begin(),v.end(),c)-v.begin();
int l,r;
int ans=0;
if(c-v[p-1]<=v[p]-c){
p--;
}
l=r=p;
ans+=abs(c-v[p]);
while(l!=1 || r!=n){
if(abs(v[p]-v[l-1])<=abs(v[r+1]-v[p])){
int a=1,b=l-1;
while(b>a){
int m=(a+b+1)/2;
if(abs(v[p]-v[r+1])>=abs(v[p]-v[m-1])){
b=m-1;
}
else{
a=m;
}
}
ans+=abs(v[p]-v[a]);
l=p=a;
}
else{
int a=r+1,b=n;
while(b>a){
int m=(a+b)/2;
if(abs(v[p]-v[l-1])>abs(v[p]-v[m+1])){
a=m+1;
}
else{
b=m;
}
}
ans+=abs(v[p]-v[a]);
r=p=a;
}
}
cout << ans << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 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 |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 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 |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
16 ms |
3796 KB |
Output is correct |
18 |
Correct |
17 ms |
3796 KB |
Output is correct |
19 |
Correct |
15 ms |
3788 KB |
Output is correct |
20 |
Correct |
16 ms |
3824 KB |
Output is correct |
21 |
Correct |
16 ms |
3796 KB |
Output is correct |
22 |
Correct |
16 ms |
3796 KB |
Output is correct |
23 |
Correct |
16 ms |
3796 KB |
Output is correct |
24 |
Correct |
15 ms |
3796 KB |
Output is correct |
25 |
Correct |
15 ms |
3800 KB |
Output is correct |
26 |
Correct |
16 ms |
3796 KB |
Output is correct |
27 |
Correct |
16 ms |
3924 KB |
Output is correct |
28 |
Correct |
18 ms |
3828 KB |
Output is correct |
29 |
Correct |
15 ms |
3656 KB |
Output is correct |
30 |
Correct |
16 ms |
3656 KB |
Output is correct |
31 |
Correct |
15 ms |
3660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
555 ms |
5820 KB |
Output is correct |
8 |
Correct |
550 ms |
5840 KB |
Output is correct |
9 |
Correct |
554 ms |
5844 KB |
Output is correct |
10 |
Correct |
548 ms |
5836 KB |
Output is correct |
11 |
Correct |
578 ms |
5724 KB |
Output is correct |
12 |
Correct |
549 ms |
5780 KB |
Output is correct |
13 |
Correct |
30 ms |
4172 KB |
Output is correct |
14 |
Correct |
22 ms |
1464 KB |
Output is correct |
15 |
Correct |
373 ms |
6792 KB |
Output is correct |
16 |
Correct |
371 ms |
6884 KB |
Output is correct |
17 |
Correct |
391 ms |
6836 KB |
Output is correct |
18 |
Correct |
31 ms |
4140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 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 |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
16 ms |
3796 KB |
Output is correct |
18 |
Correct |
17 ms |
3796 KB |
Output is correct |
19 |
Correct |
15 ms |
3788 KB |
Output is correct |
20 |
Correct |
16 ms |
3824 KB |
Output is correct |
21 |
Correct |
16 ms |
3796 KB |
Output is correct |
22 |
Correct |
16 ms |
3796 KB |
Output is correct |
23 |
Correct |
16 ms |
3796 KB |
Output is correct |
24 |
Correct |
15 ms |
3796 KB |
Output is correct |
25 |
Correct |
15 ms |
3800 KB |
Output is correct |
26 |
Correct |
16 ms |
3796 KB |
Output is correct |
27 |
Correct |
16 ms |
3924 KB |
Output is correct |
28 |
Correct |
18 ms |
3828 KB |
Output is correct |
29 |
Correct |
15 ms |
3656 KB |
Output is correct |
30 |
Correct |
16 ms |
3656 KB |
Output is correct |
31 |
Correct |
15 ms |
3660 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
555 ms |
5820 KB |
Output is correct |
39 |
Correct |
550 ms |
5840 KB |
Output is correct |
40 |
Correct |
554 ms |
5844 KB |
Output is correct |
41 |
Correct |
548 ms |
5836 KB |
Output is correct |
42 |
Correct |
578 ms |
5724 KB |
Output is correct |
43 |
Correct |
549 ms |
5780 KB |
Output is correct |
44 |
Correct |
30 ms |
4172 KB |
Output is correct |
45 |
Correct |
22 ms |
1464 KB |
Output is correct |
46 |
Correct |
373 ms |
6792 KB |
Output is correct |
47 |
Correct |
371 ms |
6884 KB |
Output is correct |
48 |
Correct |
391 ms |
6836 KB |
Output is correct |
49 |
Correct |
31 ms |
4140 KB |
Output is correct |
50 |
Correct |
595 ms |
7912 KB |
Output is correct |
51 |
Correct |
630 ms |
7836 KB |
Output is correct |
52 |
Correct |
459 ms |
7944 KB |
Output is correct |
53 |
Correct |
729 ms |
7904 KB |
Output is correct |
54 |
Correct |
694 ms |
7884 KB |
Output is correct |
55 |
Correct |
709 ms |
7960 KB |
Output is correct |
56 |
Correct |
95 ms |
7724 KB |
Output is correct |
57 |
Correct |
97 ms |
7784 KB |
Output is correct |
58 |
Correct |
100 ms |
7832 KB |
Output is correct |