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>
#define int long long
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
#define pb push_back
using namespace std;
signed main(){
int n;
cin>>n;
vector <int> a(n);
for(int i=0;i<n;i++)cin>>a[i];
int q;cin>>q;
while(q--){
int x;
cin>>x;
auto it=lower_bound(all(a),x);
int l=it-a.begin(),r=it-a.begin();
int ans=0;
while(true){
if(l==0){
ans+=(a[n-1]-x);
break;
}
else if(r==n){
ans+=(x-a[0]);
break;
}
else if(abs(a[l-1]-x)<=abs(a[r]-x)){
auto it=lower_bound(all(a),x*2-a[r]);
int pos=it-a.begin();
ans+=abs(x-a[pos]);
l=pos;
x=a[pos];
}
else{
auto it=lower_bound(all(a),x*2-a[l-1]);
int pos=it-a.begin();
r=pos;
pos--;
ans+=abs(x-a[pos]);
x=a[pos];
}
}
cout<<ans<<"\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... |