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 ll long long
#define fi first
#define se second
using namespace std ;
const ll N = 2e5 ;
ll n, q, x[N + 1] ;
signed main()
{
ios_base::sync_with_stdio( 0 ) ;
cin.tie( 0 ) ;
cout.tie( 0 ) ;
cin >> n ;
for(ll i = 1 ; i <= n ; i++)
cin >> x[i] ;
cin >> q ;
if(q == 1)
{
ll ans = 0, pos ;
cin >> pos ;
set<ll> s_neg, s_pos ;
for(ll i = 1 ; i <= n ; i++)
s_pos.insert(x[i]), s_neg.insert(-x[i]) ;
while(s_pos.size())
{
ll l = -1e18, r = 1e18 ;
if(s_pos.lower_bound(pos) != s_pos.end())
r = *s_pos.lower_bound(pos) ;
if(s_neg.lower_bound(-pos) != s_neg.end())
l = -(*s_neg.lower_bound(-pos)) ;
if(pos - l <= r - pos)
{
ans += pos - l ;
pos = l ;
s_neg.erase(-l) ;
s_pos.erase(l) ;
}
else
{
ans += r - pos ;
pos = r ;
s_neg.erase(-r) ;
s_pos.erase(r) ;
}
}
cout << ans ;
return 0 ;
}
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... |