#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) {
sum+=x;
l--;
}
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) {
sum+=x;
r++;
}
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 |
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 |
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 |
592 KB |
Output is correct |
11 |
Correct |
1 ms |
504 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
504 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 |
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 |
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 |
592 KB |
Output is correct |
11 |
Correct |
1 ms |
504 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
504 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 |
17 ms |
3664 KB |
Output is correct |
18 |
Correct |
18 ms |
1972 KB |
Output is correct |
19 |
Correct |
19 ms |
3300 KB |
Output is correct |
20 |
Correct |
19 ms |
1964 KB |
Output is correct |
21 |
Correct |
21 ms |
1872 KB |
Output is correct |
22 |
Correct |
17 ms |
1872 KB |
Output is correct |
23 |
Correct |
16 ms |
3536 KB |
Output is correct |
24 |
Correct |
17 ms |
3664 KB |
Output is correct |
25 |
Correct |
16 ms |
1872 KB |
Output is correct |
26 |
Correct |
17 ms |
2012 KB |
Output is correct |
27 |
Correct |
19 ms |
2888 KB |
Output is correct |
28 |
Correct |
19 ms |
3144 KB |
Output is correct |
29 |
Correct |
16 ms |
1872 KB |
Output is correct |
30 |
Correct |
25 ms |
2896 KB |
Output is correct |
31 |
Correct |
18 ms |
3664 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 |
348 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 |
Execution timed out |
3091 ms |
1872 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
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 |
592 KB |
Output is correct |
11 |
Correct |
1 ms |
504 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
504 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 |
17 ms |
3664 KB |
Output is correct |
18 |
Correct |
18 ms |
1972 KB |
Output is correct |
19 |
Correct |
19 ms |
3300 KB |
Output is correct |
20 |
Correct |
19 ms |
1964 KB |
Output is correct |
21 |
Correct |
21 ms |
1872 KB |
Output is correct |
22 |
Correct |
17 ms |
1872 KB |
Output is correct |
23 |
Correct |
16 ms |
3536 KB |
Output is correct |
24 |
Correct |
17 ms |
3664 KB |
Output is correct |
25 |
Correct |
16 ms |
1872 KB |
Output is correct |
26 |
Correct |
17 ms |
2012 KB |
Output is correct |
27 |
Correct |
19 ms |
2888 KB |
Output is correct |
28 |
Correct |
19 ms |
3144 KB |
Output is correct |
29 |
Correct |
16 ms |
1872 KB |
Output is correct |
30 |
Correct |
25 ms |
2896 KB |
Output is correct |
31 |
Correct |
18 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 |
348 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 |
Execution timed out |
3091 ms |
1872 KB |
Time limit exceeded |
39 |
Halted |
0 ms |
0 KB |
- |