#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";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
600 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 |
1 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 |
464 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
600 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 |
1 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 |
464 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
54 ms |
3636 KB |
Output is correct |
18 |
Correct |
71 ms |
3644 KB |
Output is correct |
19 |
Correct |
54 ms |
3772 KB |
Output is correct |
20 |
Correct |
54 ms |
3668 KB |
Output is correct |
21 |
Correct |
56 ms |
3668 KB |
Output is correct |
22 |
Correct |
61 ms |
3920 KB |
Output is correct |
23 |
Correct |
55 ms |
3664 KB |
Output is correct |
24 |
Correct |
53 ms |
3668 KB |
Output is correct |
25 |
Correct |
54 ms |
3636 KB |
Output is correct |
26 |
Correct |
55 ms |
3636 KB |
Output is correct |
27 |
Correct |
54 ms |
3648 KB |
Output is correct |
28 |
Correct |
54 ms |
3668 KB |
Output is correct |
29 |
Correct |
53 ms |
3652 KB |
Output is correct |
30 |
Correct |
53 ms |
3596 KB |
Output is correct |
31 |
Correct |
53 ms |
3740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
641 ms |
5824 KB |
Output is correct |
8 |
Correct |
644 ms |
5584 KB |
Output is correct |
9 |
Correct |
635 ms |
5572 KB |
Output is correct |
10 |
Correct |
631 ms |
5596 KB |
Output is correct |
11 |
Correct |
645 ms |
5688 KB |
Output is correct |
12 |
Correct |
631 ms |
5456 KB |
Output is correct |
13 |
Correct |
298 ms |
3924 KB |
Output is correct |
14 |
Correct |
258 ms |
1408 KB |
Output is correct |
15 |
Correct |
526 ms |
6604 KB |
Output is correct |
16 |
Correct |
531 ms |
6652 KB |
Output is correct |
17 |
Correct |
530 ms |
6592 KB |
Output is correct |
18 |
Correct |
297 ms |
4496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
600 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 |
1 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 |
464 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
54 ms |
3636 KB |
Output is correct |
18 |
Correct |
71 ms |
3644 KB |
Output is correct |
19 |
Correct |
54 ms |
3772 KB |
Output is correct |
20 |
Correct |
54 ms |
3668 KB |
Output is correct |
21 |
Correct |
56 ms |
3668 KB |
Output is correct |
22 |
Correct |
61 ms |
3920 KB |
Output is correct |
23 |
Correct |
55 ms |
3664 KB |
Output is correct |
24 |
Correct |
53 ms |
3668 KB |
Output is correct |
25 |
Correct |
54 ms |
3636 KB |
Output is correct |
26 |
Correct |
55 ms |
3636 KB |
Output is correct |
27 |
Correct |
54 ms |
3648 KB |
Output is correct |
28 |
Correct |
54 ms |
3668 KB |
Output is correct |
29 |
Correct |
53 ms |
3652 KB |
Output is correct |
30 |
Correct |
53 ms |
3596 KB |
Output is correct |
31 |
Correct |
53 ms |
3740 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
641 ms |
5824 KB |
Output is correct |
39 |
Correct |
644 ms |
5584 KB |
Output is correct |
40 |
Correct |
635 ms |
5572 KB |
Output is correct |
41 |
Correct |
631 ms |
5596 KB |
Output is correct |
42 |
Correct |
645 ms |
5688 KB |
Output is correct |
43 |
Correct |
631 ms |
5456 KB |
Output is correct |
44 |
Correct |
298 ms |
3924 KB |
Output is correct |
45 |
Correct |
258 ms |
1408 KB |
Output is correct |
46 |
Correct |
526 ms |
6604 KB |
Output is correct |
47 |
Correct |
531 ms |
6652 KB |
Output is correct |
48 |
Correct |
530 ms |
6592 KB |
Output is correct |
49 |
Correct |
297 ms |
4496 KB |
Output is correct |
50 |
Correct |
644 ms |
7456 KB |
Output is correct |
51 |
Correct |
579 ms |
7508 KB |
Output is correct |
52 |
Correct |
557 ms |
7508 KB |
Output is correct |
53 |
Correct |
724 ms |
7340 KB |
Output is correct |
54 |
Correct |
723 ms |
7492 KB |
Output is correct |
55 |
Correct |
717 ms |
7492 KB |
Output is correct |
56 |
Correct |
370 ms |
7508 KB |
Output is correct |
57 |
Correct |
366 ms |
7084 KB |
Output is correct |
58 |
Correct |
414 ms |
7264 KB |
Output is correct |