#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;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
592 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 |
336 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
592 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 |
336 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 |
15 ms |
1872 KB |
Output is correct |
18 |
Correct |
16 ms |
1872 KB |
Output is correct |
19 |
Correct |
20 ms |
1820 KB |
Output is correct |
20 |
Correct |
16 ms |
1872 KB |
Output is correct |
21 |
Correct |
16 ms |
1872 KB |
Output is correct |
22 |
Correct |
23 ms |
1872 KB |
Output is correct |
23 |
Correct |
15 ms |
1788 KB |
Output is correct |
24 |
Correct |
16 ms |
1884 KB |
Output is correct |
25 |
Correct |
15 ms |
1956 KB |
Output is correct |
26 |
Correct |
21 ms |
1872 KB |
Output is correct |
27 |
Correct |
20 ms |
1884 KB |
Output is correct |
28 |
Correct |
22 ms |
1872 KB |
Output is correct |
29 |
Correct |
14 ms |
1872 KB |
Output is correct |
30 |
Correct |
14 ms |
1872 KB |
Output is correct |
31 |
Correct |
15 ms |
1944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 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 |
341 ms |
3180 KB |
Output is correct |
8 |
Correct |
329 ms |
3396 KB |
Output is correct |
9 |
Correct |
369 ms |
3176 KB |
Output is correct |
10 |
Correct |
321 ms |
3348 KB |
Output is correct |
11 |
Correct |
325 ms |
3400 KB |
Output is correct |
12 |
Correct |
353 ms |
3400 KB |
Output is correct |
13 |
Correct |
30 ms |
2380 KB |
Output is correct |
14 |
Correct |
18 ms |
848 KB |
Output is correct |
15 |
Correct |
221 ms |
3656 KB |
Output is correct |
16 |
Correct |
237 ms |
3668 KB |
Output is correct |
17 |
Correct |
224 ms |
3596 KB |
Output is correct |
18 |
Correct |
32 ms |
2376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
592 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 |
336 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 |
15 ms |
1872 KB |
Output is correct |
18 |
Correct |
16 ms |
1872 KB |
Output is correct |
19 |
Correct |
20 ms |
1820 KB |
Output is correct |
20 |
Correct |
16 ms |
1872 KB |
Output is correct |
21 |
Correct |
16 ms |
1872 KB |
Output is correct |
22 |
Correct |
23 ms |
1872 KB |
Output is correct |
23 |
Correct |
15 ms |
1788 KB |
Output is correct |
24 |
Correct |
16 ms |
1884 KB |
Output is correct |
25 |
Correct |
15 ms |
1956 KB |
Output is correct |
26 |
Correct |
21 ms |
1872 KB |
Output is correct |
27 |
Correct |
20 ms |
1884 KB |
Output is correct |
28 |
Correct |
22 ms |
1872 KB |
Output is correct |
29 |
Correct |
14 ms |
1872 KB |
Output is correct |
30 |
Correct |
14 ms |
1872 KB |
Output is correct |
31 |
Correct |
15 ms |
1944 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 |
336 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 |
341 ms |
3180 KB |
Output is correct |
39 |
Correct |
329 ms |
3396 KB |
Output is correct |
40 |
Correct |
369 ms |
3176 KB |
Output is correct |
41 |
Correct |
321 ms |
3348 KB |
Output is correct |
42 |
Correct |
325 ms |
3400 KB |
Output is correct |
43 |
Correct |
353 ms |
3400 KB |
Output is correct |
44 |
Correct |
30 ms |
2380 KB |
Output is correct |
45 |
Correct |
18 ms |
848 KB |
Output is correct |
46 |
Correct |
221 ms |
3656 KB |
Output is correct |
47 |
Correct |
237 ms |
3668 KB |
Output is correct |
48 |
Correct |
224 ms |
3596 KB |
Output is correct |
49 |
Correct |
32 ms |
2376 KB |
Output is correct |
50 |
Correct |
341 ms |
4168 KB |
Output is correct |
51 |
Correct |
248 ms |
4168 KB |
Output is correct |
52 |
Correct |
284 ms |
4060 KB |
Output is correct |
53 |
Correct |
428 ms |
4072 KB |
Output is correct |
54 |
Correct |
435 ms |
4152 KB |
Output is correct |
55 |
Correct |
436 ms |
4576 KB |
Output is correct |
56 |
Correct |
84 ms |
4144 KB |
Output is correct |
57 |
Correct |
57 ms |
4008 KB |
Output is correct |
58 |
Correct |
58 ms |
4036 KB |
Output is correct |