#include<bits/stdc++.h>
using namespace std;
#define foru(i,a,b) for(int i=(a); i<=(b); ++i)
#define ford(i,a,b) for(int i=(a); i>=(b); --i)
#define rep(i,a) for(int i=0; i<(a); ++i)
#define sz(a) (int)(a).size()
#define all(a) (a).begin(),(a).end()
#define bit(s,i) (((s)>>(i))&1)
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define fi first
#define se second
#define int long long
#define ll long long
#define eb emplace_back
#define pb push_back
#define __builtin_popcount __builtin_popcountll
#define _ << " " <<
template <class X, class Y> bool maxi(X &x, Y y){return x<y?x=y,true:false;}
template <class X, class Y> bool mini(X &x, Y y){return x>y?x=y,true:false;}
const int N=2e5+5;
int n,q,x[N];
void solve(){
cin>>n;
foru(i,1,n) cin>>x[i];
x[0]=-1e9, x[n+1]=2e9;
cin>>q;
foru(i,1,q){
int s; cin>>s;
int p=lower_bound(x+1,x+1+n,s)-x;
if(p==1) p=1;
else if(p==n+1) p=n;
else{
if(s-x[p-1] <= x[p]-s) --p;
}
ll res=abs(x[p]-s);
int l=p-1, r=p+1;
while(l>=1 || r<=n){
if(x[p]-x[l] <= x[r]-x[p]){
int dist=x[r]-x[p];
int lm=lower_bound(x+1,x+1+n,x[p]-dist)-x;
res+=x[p]-x[lm];
l=lm-1; p=lm;
} else{
int dist=x[p]-x[l];
int lm=lower_bound(x+1,x+1+n,x[p]+dist)-x-1;
res+=x[lm]-x[p];
r=lm+1; p=lm;
}
}
cout<<res<<'\n';
}
}
int32_t main(){
#define task "test"
if(fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
cin.tie(0)->sync_with_stdio(0);
int tc=1; //cin>>tc;
foru(i,1,tc){
solve();
}
}
Compilation message (stderr)
travel.cpp: In function 'int32_t main()':
travel.cpp:68:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
travel.cpp:69:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | 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... |