#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define int long long
#define pb push_back
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define ordered_set tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define ordered_multiset tree<type1, null_type, less_equal<type1>, rb_tree_tag, tree_order_statistics_node_update>;
using namespace std;
using namespace __gnu_pbds;
const int mod = 1e9+7;
const double PI = acos(-1.0);
const double epsilon = 1e-6;
const int N = 1e5+5;
void solve(){
int n; cin >> n;
vector<int> a(n);
for(int &i : a) cin >> i;
int q; cin >> q;
while(q--){
int pos; cin >> pos;
auto lb = lower_bound(all(a),pos);
int ans = 0;
if(lb == a.begin()){
for(int i = 0; i < n-1; i++) ans += a[i+1] - a[i];
cout << ans + a[0] - pos << '\n';
continue;
}else if(lb == a.end()){
for(int i = n-1; i > 0; i--) ans += a[i] - a[i-1];
cout << ans + pos - a[n-1] << '\n';
continue;
}
int r = lb - a.begin(),l = r - 1;
while(1){
if(l < 0 && r >= n) break;
if(l >= 0 && abs(pos - a[l]) <= abs(pos - a[r])){
ans += abs(pos - a[l]);
pos = a[l];
l--;
}else{
ans += abs(pos - a[r]);
pos = a[r];
r++;
}
}
cout << ans << '\n';
}
}
main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
solve();
}
}
Compilation message
travel.cpp:58:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
58 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 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 |
420 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 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 |
420 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
15 ms |
1884 KB |
Output is correct |
18 |
Correct |
15 ms |
1884 KB |
Output is correct |
19 |
Correct |
15 ms |
1884 KB |
Output is correct |
20 |
Correct |
15 ms |
1880 KB |
Output is correct |
21 |
Correct |
15 ms |
1884 KB |
Output is correct |
22 |
Correct |
15 ms |
1884 KB |
Output is correct |
23 |
Correct |
15 ms |
1884 KB |
Output is correct |
24 |
Correct |
15 ms |
1884 KB |
Output is correct |
25 |
Correct |
15 ms |
1884 KB |
Output is correct |
26 |
Correct |
15 ms |
1884 KB |
Output is correct |
27 |
Correct |
16 ms |
1884 KB |
Output is correct |
28 |
Correct |
15 ms |
1884 KB |
Output is correct |
29 |
Correct |
15 ms |
1884 KB |
Output is correct |
30 |
Correct |
15 ms |
1884 KB |
Output is correct |
31 |
Correct |
15 ms |
1884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
7 |
Execution timed out |
3091 ms |
2356 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 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 |
420 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
15 ms |
1884 KB |
Output is correct |
18 |
Correct |
15 ms |
1884 KB |
Output is correct |
19 |
Correct |
15 ms |
1884 KB |
Output is correct |
20 |
Correct |
15 ms |
1880 KB |
Output is correct |
21 |
Correct |
15 ms |
1884 KB |
Output is correct |
22 |
Correct |
15 ms |
1884 KB |
Output is correct |
23 |
Correct |
15 ms |
1884 KB |
Output is correct |
24 |
Correct |
15 ms |
1884 KB |
Output is correct |
25 |
Correct |
15 ms |
1884 KB |
Output is correct |
26 |
Correct |
15 ms |
1884 KB |
Output is correct |
27 |
Correct |
16 ms |
1884 KB |
Output is correct |
28 |
Correct |
15 ms |
1884 KB |
Output is correct |
29 |
Correct |
15 ms |
1884 KB |
Output is correct |
30 |
Correct |
15 ms |
1884 KB |
Output is correct |
31 |
Correct |
15 ms |
1884 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
38 |
Execution timed out |
3091 ms |
2356 KB |
Time limit exceeded |
39 |
Halted |
0 ms |
0 KB |
- |