#include <bits/stdc++.h>
#define int long long
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
#define pb push_back
using namespace std;
signed main(){
int n;
cin>>n;
vector <int> a(n);
for(int i=0;i<n;i++)cin>>a[i];
int q;cin>>q;
while(q--){
int x;
cin>>x;
auto it=lower_bound(all(a),x);
int l=it-a.begin(),r=it-a.begin();
int ans=0;
while(true){
if(l==0){
ans+=(a[n-1]-x);
break;
}
else if(r==n){
ans+=(x-a[0]);
break;
}
else if(abs(a[l-1]-x)<=abs(a[r]-x)){
auto it=lower_bound(all(a),x*2-a[r]);
int pos=it-a.begin();
ans+=abs(x-a[pos]);
l=pos;
x=a[pos];
}
else{
auto it=lower_bound(all(a),x*2-a[l-1]);
int pos=it-a.begin();
r=pos;
pos--;
ans+=abs(x-a[pos]);
x=a[pos];
}
}
cout<<ans<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
472 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 |
436 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 |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
472 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 |
436 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 |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
54 ms |
3132 KB |
Output is correct |
18 |
Correct |
57 ms |
3140 KB |
Output is correct |
19 |
Correct |
54 ms |
2904 KB |
Output is correct |
20 |
Correct |
55 ms |
3060 KB |
Output is correct |
21 |
Correct |
55 ms |
3152 KB |
Output is correct |
22 |
Correct |
54 ms |
3012 KB |
Output is correct |
23 |
Correct |
54 ms |
3088 KB |
Output is correct |
24 |
Correct |
54 ms |
3024 KB |
Output is correct |
25 |
Correct |
56 ms |
3012 KB |
Output is correct |
26 |
Correct |
54 ms |
3140 KB |
Output is correct |
27 |
Correct |
54 ms |
3136 KB |
Output is correct |
28 |
Correct |
54 ms |
2908 KB |
Output is correct |
29 |
Correct |
53 ms |
3100 KB |
Output is correct |
30 |
Correct |
54 ms |
3080 KB |
Output is correct |
31 |
Correct |
53 ms |
3028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
432 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
636 ms |
5184 KB |
Output is correct |
8 |
Correct |
637 ms |
4712 KB |
Output is correct |
9 |
Correct |
632 ms |
4836 KB |
Output is correct |
10 |
Correct |
636 ms |
5080 KB |
Output is correct |
11 |
Correct |
634 ms |
4908 KB |
Output is correct |
12 |
Correct |
638 ms |
4676 KB |
Output is correct |
13 |
Correct |
300 ms |
3472 KB |
Output is correct |
14 |
Correct |
257 ms |
1364 KB |
Output is correct |
15 |
Correct |
538 ms |
5716 KB |
Output is correct |
16 |
Correct |
526 ms |
5532 KB |
Output is correct |
17 |
Correct |
534 ms |
5916 KB |
Output is correct |
18 |
Correct |
297 ms |
3516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
472 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 |
436 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 |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
54 ms |
3132 KB |
Output is correct |
18 |
Correct |
57 ms |
3140 KB |
Output is correct |
19 |
Correct |
54 ms |
2904 KB |
Output is correct |
20 |
Correct |
55 ms |
3060 KB |
Output is correct |
21 |
Correct |
55 ms |
3152 KB |
Output is correct |
22 |
Correct |
54 ms |
3012 KB |
Output is correct |
23 |
Correct |
54 ms |
3088 KB |
Output is correct |
24 |
Correct |
54 ms |
3024 KB |
Output is correct |
25 |
Correct |
56 ms |
3012 KB |
Output is correct |
26 |
Correct |
54 ms |
3140 KB |
Output is correct |
27 |
Correct |
54 ms |
3136 KB |
Output is correct |
28 |
Correct |
54 ms |
2908 KB |
Output is correct |
29 |
Correct |
53 ms |
3100 KB |
Output is correct |
30 |
Correct |
54 ms |
3080 KB |
Output is correct |
31 |
Correct |
53 ms |
3028 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
432 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
344 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
636 ms |
5184 KB |
Output is correct |
39 |
Correct |
637 ms |
4712 KB |
Output is correct |
40 |
Correct |
632 ms |
4836 KB |
Output is correct |
41 |
Correct |
636 ms |
5080 KB |
Output is correct |
42 |
Correct |
634 ms |
4908 KB |
Output is correct |
43 |
Correct |
638 ms |
4676 KB |
Output is correct |
44 |
Correct |
300 ms |
3472 KB |
Output is correct |
45 |
Correct |
257 ms |
1364 KB |
Output is correct |
46 |
Correct |
538 ms |
5716 KB |
Output is correct |
47 |
Correct |
526 ms |
5532 KB |
Output is correct |
48 |
Correct |
534 ms |
5916 KB |
Output is correct |
49 |
Correct |
297 ms |
3516 KB |
Output is correct |
50 |
Correct |
642 ms |
6484 KB |
Output is correct |
51 |
Correct |
588 ms |
6276 KB |
Output is correct |
52 |
Correct |
552 ms |
6408 KB |
Output is correct |
53 |
Correct |
725 ms |
6480 KB |
Output is correct |
54 |
Correct |
723 ms |
6372 KB |
Output is correct |
55 |
Correct |
750 ms |
6324 KB |
Output is correct |
56 |
Correct |
370 ms |
6200 KB |
Output is correct |
57 |
Correct |
368 ms |
6844 KB |
Output is correct |
58 |
Correct |
364 ms |
6336 KB |
Output is correct |