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;
#define int long long
#define all(x) x.begin(), x.end()
#define size(x) (int)x.size()
template<class S, class T>
bool chmin(S &a, const T &b) {
return a > b && (a = b) == b;
}
template<class S, class T>
bool chmax(S &a, const T &b) {
return a < b && (a = b) == b;
}
const int inf = 1e9 + 7;
const int INF = 1e18 + 7;
const int mod = 998244353;
int n;
vector<int> x;
int solve(int l, int r, bool direction) {
int p = x[direction ? r : l];
if (l == 0) {
return x[n - 1] - p;
} else if (r == n - 1) {
return p - x[0];
} else if (p - x[l - 1] <= x[r + 1] - p) {
int q = lower_bound(all(x), 2 * p - x[r + 1]) - x.begin();
return p - x[q] + solve(q, r, false);
} else {
int q = upper_bound(all(x), 2 * p - x[l - 1]) - x.begin() - 1;
return x[q] - p + solve(l, q, true);
}
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n;
x.resize(n);
for (int i = 0; i < n; ++i) {
cin >> x[i];
}
int q;
cin >> q;
while (q--) {
int s;
cin >> s;
int p = upper_bound(all(x), s) - x.begin() - 1;
if (p == -1) {
cout << x[n - 1] - s << '\n';
} else if (p == n - 1) {
cout << s - x[0] << '\n';
} else if (s - x[p] <= x[p + 1] - s) {
cout << s - x[p] + solve(p, p, false) << '\n';
} else {
cout << x[p + 1] - s + solve(p + 1, p + 1, true) << '\n';
}
}
}
# | 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... |