#include <time.h>
#include <cstdlib>
#include <stack>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <map>
#include <set>
#include <iterator>
#include <deque>
#include <queue>
#include <sstream>
#include <array>
#include <string>
#include <tuple>
#include <chrono>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <list>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <bitset>
#define ll long long
using namespace std;
int tt = 1, n;
ll a[200005], dist, x[200005];
set<ll> st;
bool ok = 0;
ll pref[200005], suff[200005];
map<ll, ll> mp;
set<pair<ll, ll>> st1;
int main(){
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
st.insert(a[i]);
st1.insert({a[i], i});
if(i > 1 && a[i] - a[i - 1] > 100) ok = 1;
mp[a[i]] = i;
}
cin >> tt;
if(!ok){
for(int i = 2; i <= n; i++)
pref[i] = pref[i - 1] + (a[i] - a[i - 1]);
for(int i = n - 1; i >= 1; i--)
suff[i] = suff[i + 1] + (a[i + 1] - a[i]);
while(tt--){
ll pos, p1, p2, sum = 0;
cin >> pos;
deque<pair<ll, int>> d1, d2;
if(pos <= a[1]){
cout << a[n] - pos << "\n";
continue;
}
if(pos >= a[n]){
cout << pos - a[1] << "\n";
continue;
}
int pos1 = *st.lower_bound(pos);
auto it = st.lower_bound(pos);
it--;
int pos2 = *it;
int t = 0;
bool f = 0;
swap(pos1, pos2);
pos1 = mp[pos1];
pos2 = mp[pos2];
if(pos - a[pos1] <= a[pos2] - pos){
sum = pos - a[pos1];
pos = a[pos1];
pos1--;
}
else{
sum = a[pos2] - pos;
pos = a[pos2];
pos2++;
f = 1;
}
while(pos1 >= 1 && pos2 <= n && a[pos2] - a[pos1] <= 100){
if(abs(pos - a[pos1]) <= abs(pos - a[pos2])){
sum += abs(pos - a[pos1]);
pos = a[pos1];
pos1--;
f = 0;
}
else{
sum += abs(pos - a[pos2]);
pos = a[pos2];
pos2++;
f = 1;
}
}
if(!f){
if(pos1 > 0) sum += (pos - a[1]);
if(pos2 != n + 1) sum += (a[n] - a[1]);
}
else{
if(pos2 <= n) sum += (a[n] - pos);
if(pos1 > 0) sum += (a[n] - a[1]);
}
cout << sum << "\n";
}
}
else{
for(int i = 1; i <= tt; i++){
cin >> x[i];
ll sum = 0, pos = x[i];
while(!st1.empty()){
ll x1 = 1e10, y1 = 1e10;
pair<ll, int> p1, p2;
if(st1.begin()->first <= pos){
auto it = st1.lower_bound({pos, 0});
it--;
x1 = pos - it->first;
p1 = *it;
}
if(st1.rbegin()->first > pos){
auto it = st1.upper_bound({pos, 0});
y1 = it->first - pos;
p2 = *it;
}
if(x1 <= y1){
sum += x1;
pos -= x1;
st1.erase(p1);
}
else{
sum += y1;
pos += y1;
st1.erase(p2);
}
}
cout << sum << "\n";
}
}
}
Compilation message
travel.cpp: In function 'int main()':
travel.cpp:55:21: warning: unused variable 'p1' [-Wunused-variable]
55 | ll pos, p1, p2, sum = 0;
| ^~
travel.cpp:55:25: warning: unused variable 'p2' [-Wunused-variable]
55 | ll pos, p1, p2, sum = 0;
| ^~
travel.cpp:70:17: warning: unused variable 't' [-Wunused-variable]
70 | int t = 0;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
2 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2516 KB |
Output is correct |
14 |
Correct |
2 ms |
2648 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
16 |
Correct |
2 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
2 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2516 KB |
Output is correct |
14 |
Correct |
2 ms |
2648 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
16 |
Correct |
2 ms |
2652 KB |
Output is correct |
17 |
Correct |
164 ms |
39720 KB |
Output is correct |
18 |
Correct |
180 ms |
39744 KB |
Output is correct |
19 |
Correct |
153 ms |
39588 KB |
Output is correct |
20 |
Correct |
161 ms |
39688 KB |
Output is correct |
21 |
Correct |
185 ms |
39764 KB |
Output is correct |
22 |
Correct |
163 ms |
40064 KB |
Output is correct |
23 |
Correct |
198 ms |
40256 KB |
Output is correct |
24 |
Correct |
178 ms |
40048 KB |
Output is correct |
25 |
Correct |
173 ms |
40292 KB |
Output is correct |
26 |
Correct |
168 ms |
40144 KB |
Output is correct |
27 |
Correct |
182 ms |
40292 KB |
Output is correct |
28 |
Correct |
191 ms |
40292 KB |
Output is correct |
29 |
Correct |
161 ms |
40168 KB |
Output is correct |
30 |
Correct |
222 ms |
40056 KB |
Output is correct |
31 |
Correct |
176 ms |
40280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2656 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Incorrect |
426 ms |
43920 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
2 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2516 KB |
Output is correct |
14 |
Correct |
2 ms |
2648 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
16 |
Correct |
2 ms |
2652 KB |
Output is correct |
17 |
Correct |
164 ms |
39720 KB |
Output is correct |
18 |
Correct |
180 ms |
39744 KB |
Output is correct |
19 |
Correct |
153 ms |
39588 KB |
Output is correct |
20 |
Correct |
161 ms |
39688 KB |
Output is correct |
21 |
Correct |
185 ms |
39764 KB |
Output is correct |
22 |
Correct |
163 ms |
40064 KB |
Output is correct |
23 |
Correct |
198 ms |
40256 KB |
Output is correct |
24 |
Correct |
178 ms |
40048 KB |
Output is correct |
25 |
Correct |
173 ms |
40292 KB |
Output is correct |
26 |
Correct |
168 ms |
40144 KB |
Output is correct |
27 |
Correct |
182 ms |
40292 KB |
Output is correct |
28 |
Correct |
191 ms |
40292 KB |
Output is correct |
29 |
Correct |
161 ms |
40168 KB |
Output is correct |
30 |
Correct |
222 ms |
40056 KB |
Output is correct |
31 |
Correct |
176 ms |
40280 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
1 ms |
2656 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2392 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Incorrect |
426 ms |
43920 KB |
Output isn't correct |
39 |
Halted |
0 ms |
0 KB |
- |