#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/extc++.h>
#define all(v) v.begin(), v.end()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
ll n, q;
void solve() {
cin>>n;
vector<ll> x(n);
for(ll& xx: x) cin>>xx;
cin>>q;
while(q --> 0) {
ll s; cin>>s;
ll idx=lower_bound(all(x),s)-x.begin(), ans=0;
ll l, r;
if(idx==0) {
cout<<x[n-1]-s<<'\n';
continue;
}
else if(idx==n) {
cout<<s-x[0]<<'\n';
continue;
}
else {
ll rd=x[idx]-s, ld=s-x[idx-1];
if(ld<=rd) ans+=ld, l=r=idx-1, s=x[idx-1];
else ans+=rd, l=r=idx, s=x[idx];
}
//l~r까지 완료 함...
while(1) {
if(l==0) {
ans+=x[n-1]-s;
break;
}
if(r==n-1) {
ans+=s-x[0];
break;
}
bool chk=false;
if(l==1 && r==3) chk=true;
if(s-x[l-1]<=x[r+1]-s) {//좌
ll i=lower_bound(all(x),2*s-x[r+1])-x.begin();
ans+=s-x[i];
s=x[i];
l=i;
}
else {
ll i=lower_bound(all(x),2*s-x[l-1])-x.begin()-1;
//if(i==n) i--;
//if(chk) cout<<i<<endl;
ans+=x[i]-s;
s=x[i];
r=i;
}
if(chk) {
//cout<<ans<<' '<<l<<' '<<r<<' '<<s<<endl;
//return;
}
}
cout<<ans<<'\n';
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int tc=1; //cin>>tc;
while(tc--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
456 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
456 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 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 |
16 ms |
3932 KB |
Output is correct |
18 |
Correct |
16 ms |
3940 KB |
Output is correct |
19 |
Correct |
16 ms |
3920 KB |
Output is correct |
20 |
Correct |
16 ms |
3932 KB |
Output is correct |
21 |
Correct |
20 ms |
3932 KB |
Output is correct |
22 |
Correct |
16 ms |
3808 KB |
Output is correct |
23 |
Correct |
16 ms |
3940 KB |
Output is correct |
24 |
Correct |
20 ms |
3940 KB |
Output is correct |
25 |
Correct |
16 ms |
3928 KB |
Output is correct |
26 |
Correct |
16 ms |
4128 KB |
Output is correct |
27 |
Correct |
16 ms |
3932 KB |
Output is correct |
28 |
Correct |
16 ms |
3932 KB |
Output is correct |
29 |
Correct |
16 ms |
3676 KB |
Output is correct |
30 |
Correct |
16 ms |
3688 KB |
Output is correct |
31 |
Correct |
15 ms |
3676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
341 ms |
3164 KB |
Output is correct |
8 |
Correct |
342 ms |
3192 KB |
Output is correct |
9 |
Correct |
340 ms |
3404 KB |
Output is correct |
10 |
Correct |
344 ms |
3412 KB |
Output is correct |
11 |
Correct |
340 ms |
3408 KB |
Output is correct |
12 |
Correct |
343 ms |
3384 KB |
Output is correct |
13 |
Correct |
32 ms |
2388 KB |
Output is correct |
14 |
Correct |
24 ms |
856 KB |
Output is correct |
15 |
Correct |
236 ms |
3720 KB |
Output is correct |
16 |
Correct |
226 ms |
6984 KB |
Output is correct |
17 |
Correct |
226 ms |
7000 KB |
Output is correct |
18 |
Correct |
34 ms |
4176 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 |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
456 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 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 |
16 ms |
3932 KB |
Output is correct |
18 |
Correct |
16 ms |
3940 KB |
Output is correct |
19 |
Correct |
16 ms |
3920 KB |
Output is correct |
20 |
Correct |
16 ms |
3932 KB |
Output is correct |
21 |
Correct |
20 ms |
3932 KB |
Output is correct |
22 |
Correct |
16 ms |
3808 KB |
Output is correct |
23 |
Correct |
16 ms |
3940 KB |
Output is correct |
24 |
Correct |
20 ms |
3940 KB |
Output is correct |
25 |
Correct |
16 ms |
3928 KB |
Output is correct |
26 |
Correct |
16 ms |
4128 KB |
Output is correct |
27 |
Correct |
16 ms |
3932 KB |
Output is correct |
28 |
Correct |
16 ms |
3932 KB |
Output is correct |
29 |
Correct |
16 ms |
3676 KB |
Output is correct |
30 |
Correct |
16 ms |
3688 KB |
Output is correct |
31 |
Correct |
15 ms |
3676 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
341 ms |
3164 KB |
Output is correct |
39 |
Correct |
342 ms |
3192 KB |
Output is correct |
40 |
Correct |
340 ms |
3404 KB |
Output is correct |
41 |
Correct |
344 ms |
3412 KB |
Output is correct |
42 |
Correct |
340 ms |
3408 KB |
Output is correct |
43 |
Correct |
343 ms |
3384 KB |
Output is correct |
44 |
Correct |
32 ms |
2388 KB |
Output is correct |
45 |
Correct |
24 ms |
856 KB |
Output is correct |
46 |
Correct |
236 ms |
3720 KB |
Output is correct |
47 |
Correct |
226 ms |
6984 KB |
Output is correct |
48 |
Correct |
226 ms |
7000 KB |
Output is correct |
49 |
Correct |
34 ms |
4176 KB |
Output is correct |
50 |
Correct |
358 ms |
8032 KB |
Output is correct |
51 |
Correct |
254 ms |
8016 KB |
Output is correct |
52 |
Correct |
239 ms |
7904 KB |
Output is correct |
53 |
Correct |
427 ms |
8120 KB |
Output is correct |
54 |
Correct |
400 ms |
7896 KB |
Output is correct |
55 |
Correct |
402 ms |
8020 KB |
Output is correct |
56 |
Correct |
57 ms |
7760 KB |
Output is correct |
57 |
Correct |
58 ms |
7760 KB |
Output is correct |
58 |
Correct |
57 ms |
7896 KB |
Output is correct |