#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("Ofast,unroll-loops")
#define pii pair<int,int>
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define int long long
#define f first
#define s second
#define pii pair<int,int>
template<class T>bool umax(T &a,T b){if(a<b){a=b;return true;}return false;}
template<class T>bool umin(T &a,T b){if(b<a){a=b;return true;}return false;}
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
tree_order_statistics_node_update> ordered_set;
const int mod= 1e9 +7;
const int N=1e5*4;
int binpow (int a, int n) {
if (n == 0)
return 1;
if (n % 2 == 1)
return binpow (a, n-1) * a;
else {
int b = binpow (a, n/2);
return b * b;
}
}
void solve(){
int n,m,k;
cin>>n;
vector<int>v(n);
for(auto &to:v)cin>>to;
int q;
cin>>q;
while(q--){
int pos;
cin>>pos;
int l = lower_bound(all(v),pos) - v.begin();
int r = l;
int ans = 0;
while(1){
if(l == 0){
ans += abs(v[n-1] - pos);
break;
}
if(r == n){
ans += abs(pos - v[0]);
break;
}
if(pos - v[l-1] <= v[r] - pos){
int i = lower_bound(all(v), 2*pos-v[r]) - v.begin();
ans += pos-v[i];
pos = v[i];
l = i;
}
else{
int i = lower_bound(all(v), 2*pos - v[l-1]) - v.begin() - 1;
ans += v[i]-pos;
pos = v[i];
r = i+1;
}
}
cout<<ans<<"\n";
}
}
signed main()
{
// freopen("seq.in", "r", stdin);
// freopen("seq.out", "w", stdout);
ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
int tt=1;//cin>>tt;
while(tt--)solve();
}
Compilation message
travel.cpp: In function 'void solve()':
travel.cpp:36:8: warning: unused variable 'm' [-Wunused-variable]
36 | int n,m,k;
| ^
travel.cpp:36:10: warning: unused variable 'k' [-Wunused-variable]
36 | int n,m,k;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
488 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
464 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
488 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
464 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
16 ms |
3932 KB |
Output is correct |
18 |
Correct |
15 ms |
3932 KB |
Output is correct |
19 |
Correct |
15 ms |
3932 KB |
Output is correct |
20 |
Correct |
16 ms |
3812 KB |
Output is correct |
21 |
Correct |
16 ms |
3940 KB |
Output is correct |
22 |
Correct |
16 ms |
3936 KB |
Output is correct |
23 |
Correct |
16 ms |
3932 KB |
Output is correct |
24 |
Correct |
16 ms |
4176 KB |
Output is correct |
25 |
Correct |
16 ms |
3928 KB |
Output is correct |
26 |
Correct |
16 ms |
4180 KB |
Output is correct |
27 |
Correct |
21 ms |
3916 KB |
Output is correct |
28 |
Correct |
16 ms |
3932 KB |
Output is correct |
29 |
Correct |
15 ms |
3812 KB |
Output is correct |
30 |
Correct |
16 ms |
3804 KB |
Output is correct |
31 |
Correct |
15 ms |
3676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
452 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
360 ms |
5916 KB |
Output is correct |
8 |
Correct |
364 ms |
5712 KB |
Output is correct |
9 |
Correct |
361 ms |
5716 KB |
Output is correct |
10 |
Correct |
362 ms |
5936 KB |
Output is correct |
11 |
Correct |
362 ms |
5856 KB |
Output is correct |
12 |
Correct |
365 ms |
5796 KB |
Output is correct |
13 |
Correct |
31 ms |
4188 KB |
Output is correct |
14 |
Correct |
23 ms |
1440 KB |
Output is correct |
15 |
Correct |
232 ms |
7396 KB |
Output is correct |
16 |
Correct |
231 ms |
6768 KB |
Output is correct |
17 |
Correct |
232 ms |
6992 KB |
Output is correct |
18 |
Correct |
31 ms |
4280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
488 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
464 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
16 ms |
3932 KB |
Output is correct |
18 |
Correct |
15 ms |
3932 KB |
Output is correct |
19 |
Correct |
15 ms |
3932 KB |
Output is correct |
20 |
Correct |
16 ms |
3812 KB |
Output is correct |
21 |
Correct |
16 ms |
3940 KB |
Output is correct |
22 |
Correct |
16 ms |
3936 KB |
Output is correct |
23 |
Correct |
16 ms |
3932 KB |
Output is correct |
24 |
Correct |
16 ms |
4176 KB |
Output is correct |
25 |
Correct |
16 ms |
3928 KB |
Output is correct |
26 |
Correct |
16 ms |
4180 KB |
Output is correct |
27 |
Correct |
21 ms |
3916 KB |
Output is correct |
28 |
Correct |
16 ms |
3932 KB |
Output is correct |
29 |
Correct |
15 ms |
3812 KB |
Output is correct |
30 |
Correct |
16 ms |
3804 KB |
Output is correct |
31 |
Correct |
15 ms |
3676 KB |
Output is correct |
32 |
Correct |
0 ms |
344 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
452 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
360 ms |
5916 KB |
Output is correct |
39 |
Correct |
364 ms |
5712 KB |
Output is correct |
40 |
Correct |
361 ms |
5716 KB |
Output is correct |
41 |
Correct |
362 ms |
5936 KB |
Output is correct |
42 |
Correct |
362 ms |
5856 KB |
Output is correct |
43 |
Correct |
365 ms |
5796 KB |
Output is correct |
44 |
Correct |
31 ms |
4188 KB |
Output is correct |
45 |
Correct |
23 ms |
1440 KB |
Output is correct |
46 |
Correct |
232 ms |
7396 KB |
Output is correct |
47 |
Correct |
231 ms |
6768 KB |
Output is correct |
48 |
Correct |
232 ms |
6992 KB |
Output is correct |
49 |
Correct |
31 ms |
4280 KB |
Output is correct |
50 |
Correct |
369 ms |
7828 KB |
Output is correct |
51 |
Correct |
271 ms |
7936 KB |
Output is correct |
52 |
Correct |
249 ms |
8016 KB |
Output is correct |
53 |
Correct |
413 ms |
7956 KB |
Output is correct |
54 |
Correct |
415 ms |
8020 KB |
Output is correct |
55 |
Correct |
411 ms |
8012 KB |
Output is correct |
56 |
Correct |
60 ms |
7504 KB |
Output is correct |
57 |
Correct |
59 ms |
7580 KB |
Output is correct |
58 |
Correct |
59 ms |
7728 KB |
Output is correct |