이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |