#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5;
int a[N+1];
int n,q;
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
// freopen("ex_interval3.in", "r", stdin);
cin >> n;
for(int i=1;i<=n;++i) cin >> a[i];
cin >> q;
while(q--)
{
int x;
cin >> x;
int id = lower_bound(a+1,a+n+1,x)-a;
int sum=0;
if(id==1)
{
cout << a[n]-x << '\n';
continue;
}
if(id==n+1)
{
cout << x-a[1] << '\n';
continue;
}
int l=id-1;
int r=id;
int now;
if(x-a[l]<=a[r]-x)
{
now=0;
r=l;
sum=x-a[l];
}
else
{
now=1;
l=r;
sum=a[r]-x;
}
bool done=false;
while(l>1 || r<n)
{
if(r==n || l==1)
{
cout << sum+(a[n]-a[1]) << '\n';
done=true;
break;
}
if(now==0)
{
int x = a[l]-a[l-1];
int id = lower_bound(a+r+1,a+n+1,a[l]+x)-a-1;
if(id==r) {
int x =a[r+1]-a[l];
int id2 = lower_bound(a+1,a+l,a[l]-x)-a;
sum+=a[l]-a[id2];
l=id2;
}
else
{
sum+=a[id]-a[l];
r=id;
now=1;
}
}
else
{
int x = a[r+1]-a[r];
int id = lower_bound(a+1,a+l,a[r]-x)-a;
if(id==l) {
int x = a[r]-a[l-1];
int id2 = lower_bound(a+r+1,a+n+1,a[r]+x)-a-1;
sum+=a[id2]-a[r];
r=id2;
}
else
{
sum+=a[r]-a[id];
l=id;
now=0;
}
}
}
}
}
//9 4
//2 5
//1 7
//6 7
//1 4
//3 5
//0 8
//1 7
//1 2
//9 10
Compilation message
travel.cpp: In function 'int32_t main()':
travel.cpp:49:14: warning: variable 'done' set but not used [-Wunused-but-set-variable]
49 | bool done=false;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
500 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
500 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
18 ms |
3848 KB |
Output is correct |
18 |
Correct |
17 ms |
3920 KB |
Output is correct |
19 |
Correct |
19 ms |
3796 KB |
Output is correct |
20 |
Correct |
16 ms |
3920 KB |
Output is correct |
21 |
Correct |
24 ms |
3320 KB |
Output is correct |
22 |
Correct |
17 ms |
2864 KB |
Output is correct |
23 |
Correct |
15 ms |
3920 KB |
Output is correct |
24 |
Correct |
18 ms |
3920 KB |
Output is correct |
25 |
Correct |
17 ms |
3916 KB |
Output is correct |
26 |
Correct |
18 ms |
3144 KB |
Output is correct |
27 |
Correct |
16 ms |
3920 KB |
Output is correct |
28 |
Correct |
15 ms |
3748 KB |
Output is correct |
29 |
Correct |
19 ms |
3664 KB |
Output is correct |
30 |
Correct |
20 ms |
3676 KB |
Output is correct |
31 |
Correct |
15 ms |
3664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
504 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
390 ms |
5960 KB |
Output is correct |
8 |
Correct |
334 ms |
5960 KB |
Output is correct |
9 |
Correct |
385 ms |
5952 KB |
Output is correct |
10 |
Correct |
342 ms |
5916 KB |
Output is correct |
11 |
Correct |
332 ms |
5940 KB |
Output is correct |
12 |
Correct |
324 ms |
5900 KB |
Output is correct |
13 |
Correct |
27 ms |
4168 KB |
Output is correct |
14 |
Correct |
18 ms |
1612 KB |
Output is correct |
15 |
Correct |
227 ms |
6884 KB |
Output is correct |
16 |
Correct |
220 ms |
6984 KB |
Output is correct |
17 |
Correct |
246 ms |
6984 KB |
Output is correct |
18 |
Correct |
31 ms |
4296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
500 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
18 ms |
3848 KB |
Output is correct |
18 |
Correct |
17 ms |
3920 KB |
Output is correct |
19 |
Correct |
19 ms |
3796 KB |
Output is correct |
20 |
Correct |
16 ms |
3920 KB |
Output is correct |
21 |
Correct |
24 ms |
3320 KB |
Output is correct |
22 |
Correct |
17 ms |
2864 KB |
Output is correct |
23 |
Correct |
15 ms |
3920 KB |
Output is correct |
24 |
Correct |
18 ms |
3920 KB |
Output is correct |
25 |
Correct |
17 ms |
3916 KB |
Output is correct |
26 |
Correct |
18 ms |
3144 KB |
Output is correct |
27 |
Correct |
16 ms |
3920 KB |
Output is correct |
28 |
Correct |
15 ms |
3748 KB |
Output is correct |
29 |
Correct |
19 ms |
3664 KB |
Output is correct |
30 |
Correct |
20 ms |
3676 KB |
Output is correct |
31 |
Correct |
15 ms |
3664 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
504 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
336 KB |
Output is correct |
37 |
Correct |
1 ms |
336 KB |
Output is correct |
38 |
Correct |
390 ms |
5960 KB |
Output is correct |
39 |
Correct |
334 ms |
5960 KB |
Output is correct |
40 |
Correct |
385 ms |
5952 KB |
Output is correct |
41 |
Correct |
342 ms |
5916 KB |
Output is correct |
42 |
Correct |
332 ms |
5940 KB |
Output is correct |
43 |
Correct |
324 ms |
5900 KB |
Output is correct |
44 |
Correct |
27 ms |
4168 KB |
Output is correct |
45 |
Correct |
18 ms |
1612 KB |
Output is correct |
46 |
Correct |
227 ms |
6884 KB |
Output is correct |
47 |
Correct |
220 ms |
6984 KB |
Output is correct |
48 |
Correct |
246 ms |
6984 KB |
Output is correct |
49 |
Correct |
31 ms |
4296 KB |
Output is correct |
50 |
Correct |
333 ms |
8008 KB |
Output is correct |
51 |
Correct |
265 ms |
8008 KB |
Output is correct |
52 |
Correct |
242 ms |
8008 KB |
Output is correct |
53 |
Correct |
439 ms |
7864 KB |
Output is correct |
54 |
Correct |
452 ms |
8044 KB |
Output is correct |
55 |
Correct |
434 ms |
8020 KB |
Output is correct |
56 |
Correct |
60 ms |
7752 KB |
Output is correct |
57 |
Correct |
63 ms |
7752 KB |
Output is correct |
58 |
Correct |
58 ms |
7752 KB |
Output is correct |