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 ff first
#define ss second
#define all(a) a.begin(), a.end()
const int N = 120000;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
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;
int it = upper_bound(all(a), x) - a.begin();
int it1 = it - 1;
int sum = 0;
while(true){
if(it >= n && it1 < 0) break;
if(it1 < 0){
sum+= abs(a.back() - x);
break;
}else if(it >= n){
sum+= abs(a[0] - x);
break;
}else if(abs(a[it1] - x) > abs(a[it] - x)){
sum+= abs(a[it] - x);
x = a[it];
it++;
}else{
sum+= abs(a[it1] - x);
x = a[it1];
it1--;
}
}
cout << sum << '\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... |