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 all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long
using i64 = long long;
template <class F, class _S>
bool chmin(F &u, const _S &v){
bool flag = false;
if ( u > v ){
u = v; flag |= true;
}
return flag;
}
template <class F, class _S>
bool chmax(F &u, const _S &v){
bool flag = false;
if ( u < v ){
u = v; flag |= true;
}
return flag;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
vector <int> X(n);
for ( auto &u: X ) cin >> u;
int q; cin >> q;
vector <int> S(q);
for ( auto &u: S ) cin >> u;
if ( q == 1 ){
int x = S[0];
const int inf = 1e15;
set <int> st{-inf, inf};
for ( int i = 0; i < n; i++ ){
st.insert(X[i]);
}
int ans = 0;
while ( st.size() > 2 ){
auto it = st.lower_bound(x);
assert(*prev(it) <= x && *it >= x);
if ( abs(*prev(it) - x) <= abs(*it - x) ){
ans += x - *prev(it);
x = *prev(it);
st.erase(prev(it));
} else{
ans += -x + *it;
x = *it;
st.erase(it);
}
}
cout << ans << ln;
}
cout << '\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... |