This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#ifdef Home
#define _GLIBCXX_DEBUG
#endif // Home
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
main() {
#ifdef Home
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif // Home
ios_base::sync_with_stdio(0);
cin.tie(0);
ll n, q, x, ans, l, r;
cin >> n;
vector < int > a(n);
for(auto &i : a) {
cin >> i;
}
for(cin >> q; q --> 0;) {
cin >> x;
r = upper_bound(a.begin(), a.end(), x) - a.begin(), l = r - 1;
ans = 0;
for(int t = n; t --> 0;) {
if(l < 0) {
ans += a[r] - x;
x = a[r];
++ r;
} else if(r == n) {
ans += x - a[l];
x = a[l];
-- l;
} else {
if(x - a[l] <= a[r] - x) {
ans += x - a[l];
x = a[l];
-- l;
} else {
ans += a[r] - x;
x = a[r];
++ r;
}
}
}
cout << ans << '\n';
}
}
Compilation message (stderr)
travel.cpp:12:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
12 | main() {
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |