#include <bits/stdc++.h>
using namespace std;/*
<<<<It's never too late for a new beginning in your life>>>>
Today is hard
tomorrow will worse
but the day after tomorrow will be the sunshine..
HARD WORK BEATS TALENT WHEN TALENT DOESN'T WORK HARD............
Never give up */
//The most CHALISHKANCHIK
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define int long long
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pair<int,int> > vii;
const long long N = 1e5+50, inf = 1e18, mod = 1e9+7;
void solve(){
int n;
cin >> n;
vi v(n);
for(int i = 0; i < n; i++){
cin >> v[i];
}
int q;
cin >> q;
while(q--){
int x, ans = 0;
cin >> x;
int l = lower_bound(all(v),x) - v.begin();
int r = l;
while(1){
if(l == 0){
ans += abs(v[n-1] - x);
break;
}
if(r == n){
ans += abs(x - v[0]);
break;
}
if(x - v[l-1] <= v[r] - x){
int i = lower_bound(all(v), 2*x-v[r]) - v.begin();
ans += x-v[i];
x = v[i];
l = i;
}
else{
int i = lower_bound(all(v), 2*x - v[l-1]) - v.begin() - 1;
ans += v[i]-x;
x = v[i];
r = i+1;
}
}
cout << ans << '\n';
}
}
main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int t = 1;
//~ cin >> t;
while(t--){
solve();
}
}
Compilation message
travel.cpp:60:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
60 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
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 |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 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 |
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 |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 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 |
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 |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
460 KB |
Output is correct |
17 |
Correct |
15 ms |
3920 KB |
Output is correct |
18 |
Correct |
15 ms |
4184 KB |
Output is correct |
19 |
Correct |
15 ms |
3916 KB |
Output is correct |
20 |
Correct |
19 ms |
4084 KB |
Output is correct |
21 |
Correct |
15 ms |
3932 KB |
Output is correct |
22 |
Correct |
18 ms |
3920 KB |
Output is correct |
23 |
Correct |
16 ms |
3808 KB |
Output is correct |
24 |
Correct |
16 ms |
3912 KB |
Output is correct |
25 |
Correct |
16 ms |
3932 KB |
Output is correct |
26 |
Correct |
16 ms |
3932 KB |
Output is correct |
27 |
Correct |
18 ms |
4188 KB |
Output is correct |
28 |
Correct |
15 ms |
3940 KB |
Output is correct |
29 |
Correct |
17 ms |
3676 KB |
Output is correct |
30 |
Correct |
15 ms |
3788 KB |
Output is correct |
31 |
Correct |
15 ms |
3680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
379 ms |
5712 KB |
Output is correct |
8 |
Correct |
354 ms |
6012 KB |
Output is correct |
9 |
Correct |
354 ms |
5928 KB |
Output is correct |
10 |
Correct |
362 ms |
5716 KB |
Output is correct |
11 |
Correct |
353 ms |
5836 KB |
Output is correct |
12 |
Correct |
356 ms |
5724 KB |
Output is correct |
13 |
Correct |
33 ms |
4336 KB |
Output is correct |
14 |
Correct |
22 ms |
1488 KB |
Output is correct |
15 |
Correct |
238 ms |
6964 KB |
Output is correct |
16 |
Correct |
239 ms |
6888 KB |
Output is correct |
17 |
Correct |
226 ms |
6940 KB |
Output is correct |
18 |
Correct |
31 ms |
4176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 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 |
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 |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
460 KB |
Output is correct |
17 |
Correct |
15 ms |
3920 KB |
Output is correct |
18 |
Correct |
15 ms |
4184 KB |
Output is correct |
19 |
Correct |
15 ms |
3916 KB |
Output is correct |
20 |
Correct |
19 ms |
4084 KB |
Output is correct |
21 |
Correct |
15 ms |
3932 KB |
Output is correct |
22 |
Correct |
18 ms |
3920 KB |
Output is correct |
23 |
Correct |
16 ms |
3808 KB |
Output is correct |
24 |
Correct |
16 ms |
3912 KB |
Output is correct |
25 |
Correct |
16 ms |
3932 KB |
Output is correct |
26 |
Correct |
16 ms |
3932 KB |
Output is correct |
27 |
Correct |
18 ms |
4188 KB |
Output is correct |
28 |
Correct |
15 ms |
3940 KB |
Output is correct |
29 |
Correct |
17 ms |
3676 KB |
Output is correct |
30 |
Correct |
15 ms |
3788 KB |
Output is correct |
31 |
Correct |
15 ms |
3680 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 |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
379 ms |
5712 KB |
Output is correct |
39 |
Correct |
354 ms |
6012 KB |
Output is correct |
40 |
Correct |
354 ms |
5928 KB |
Output is correct |
41 |
Correct |
362 ms |
5716 KB |
Output is correct |
42 |
Correct |
353 ms |
5836 KB |
Output is correct |
43 |
Correct |
356 ms |
5724 KB |
Output is correct |
44 |
Correct |
33 ms |
4336 KB |
Output is correct |
45 |
Correct |
22 ms |
1488 KB |
Output is correct |
46 |
Correct |
238 ms |
6964 KB |
Output is correct |
47 |
Correct |
239 ms |
6888 KB |
Output is correct |
48 |
Correct |
226 ms |
6940 KB |
Output is correct |
49 |
Correct |
31 ms |
4176 KB |
Output is correct |
50 |
Correct |
331 ms |
8028 KB |
Output is correct |
51 |
Correct |
278 ms |
8100 KB |
Output is correct |
52 |
Correct |
246 ms |
8020 KB |
Output is correct |
53 |
Correct |
418 ms |
8032 KB |
Output is correct |
54 |
Correct |
409 ms |
8044 KB |
Output is correct |
55 |
Correct |
409 ms |
8012 KB |
Output is correct |
56 |
Correct |
60 ms |
7860 KB |
Output is correct |
57 |
Correct |
76 ms |
7892 KB |
Output is correct |
58 |
Correct |
61 ms |
7720 KB |
Output is correct |