#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define eb emplace_back
#define pu push
#define ins insert
#define fi first
#define se second
#define all(a) a.begin(),a.end()
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define int ll
using namespace std;
//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
typedef pair<int, int> pii;
const int mod = 2147483647;
const int inf = 1e9 + 7;
const int N = 2e5 + 5;
ll x[N];
signed main(){
fastio
int n; cin >> n;
for(int i = 1; i <= n; i++) cin >> x[i];
int q; cin >> q;
while(q--){
ll s; cin >> s;
int l, r;
int pos = lower_bound(x + 1, x + n + 1, s) - x;
if(pos == n + 1) l = r = pos - 1;
else if(x[pos] == s || pos == 1 || abs(x[pos] - s) <= abs(x[pos - 1] - s)){
l = r = pos;
}
else l = r = pos - 1;
ll ans = abs(x[l] - s);
s = x[l];
while(true){
if(l == 1){
ans += abs(x[n] - s);
break;
}
if(r == n){
ans += abs(s - x[1]);
break;
}
int le = l - 1, ri = r + 1;
if(abs(x[le] - s) <= abs(x[ri] - s)){
int p = lower_bound(x + 1, x + n + 1, 2 * s - x[ri]) - x;
ans += abs(s - x[p]);
s = x[p];
l = p;
continue;
}
else{
int p = upper_bound(x + 1, x + n + 1, 2 * s - x[le]) - x - 1;
ans += abs(s - x[p]);
s = x[p];
r = p;
continue;
}
}
cout << ans << '\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... |