This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 2e5 + 5;
int n, q;
int x[N];
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> x[i];
}
cin >> q;
while (q--) {
int s; cin >> s;
int it = lower_bound(x + 1, x + n + 1, s) - x;
int l, r;
if (it == n + 1) {
l = r = n;
} else if (x[it] == s || it == 1) {
l = r = it;
} else if (x[it] - s < s - x[it - 1]) {
l = r = it;
} else {
l = r = it - 1;
}
long long res = abs(s - x[l]);
s = x[l];
while (1) {
if (l == 1) {
res += abs(s - x[n]);
break;
}
if (r == n) {
res += abs(s - x[1]);
break;
}
int L = x[l - 1], R = x[r + 1];
if (s - L <= R - s) {
int nxt = lower_bound(x + 1, x + n + 1, 2 * s - R) - x;
res += abs(s - x[nxt]);
s = x[nxt];
l = nxt;
} else {
int nxt = upper_bound(x + 1, x + n + 1, 2 * s - L) - x - 1;
res += abs(s - x[nxt]);
s = x[nxt];
r = nxt;
}
}
cout << res << "\n";
}
return 0;
}
# | 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... |