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 <iostream>
#include <bits/stdc++.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <deque>
#include <set>
#include <stack>
#include <string>
#include <map>
#include <queue>
using namespace std;
const long long oo = 1000000000000000000;
long long int sum, ans = 0, mx = 0, mn = 1000000000, num, pos;
/*
ViHHiPuh
(( `'-""``""-'` ))
)-__-_.._-__-(
/ --- (o _ o) --- \
\ .-* ( .0. ) *-. /
_'-. ,_ '=' _, .-'_
/ `;#'#'# - #'#'#;` \
\_)) -----'#'----- ((_/
# --------- #
'# ------- ------ #'
/..-'# ------- #'-.\
_\...-\'# -- #'/-.../_
((____)- '#' -(____))
cout << fixed << setprecision(6) << x;
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
freopen ( "sum.in", "r", stdin )
*/
bool bl[ 1000001 ];
set <pair<int, int> > st;
int d( int x, int y, int xx, int yy ) {
return abs( x - xx ) + abs( y - yy );
}
int main () {
int n;
cin >> n;
pair<int, int> a[ n + n + 1 ];
for ( int i = 1; i <= n; i ++ ) {
for ( int j = 1; j <= 2; j ++ ) {
st.insert( { i, j } );
}
}
int y, x;
for ( int i = 1; i <= n + n; i ++ )
cin >> a[ i ].first >> a[ i ].second;
sort( a + 1, a + n + n + 1 );
for ( int i = 1; i <= n + n; i ++ ) {
if ( a[ i ].second <= 1 ) {
y = 1;
if ( a[ i ].first >= 1 && a[ i ].first <= n )
x = a[ i ].first;
else {
if ( a[ i ].first < 1 )
x = 1;
else
x = n;
}
}
else {
y = 2;
if ( a[ i ].first >= 1 && a[ i ].first <= n )
x = a[ i ].first;
else {
if ( a[ i ].first < 1 )
x = 1;
else
x = n;
}
}
auto it = st.lower_bound( { x, y } );
if ( it != st.end() && it->first == x && it->second == y ) {
ans += d( x, y, a[ i ].first, a[ i ].second );
st.erase( it );
bl[ i ] = 1;
}
}
for ( int i = 1; i <= n + n; i ++ ) {
if ( bl[ i ] )
continue;
if ( a[ i ].second <= 1 ) {
y = 1;
if ( a[ i ].first >= 1 && a[ i ].first <= n )
x = a[ i ].first;
else {
if ( a[ i ].first < 1 )
x = 1;
else
x = n;
}
}
else {
y = 2;
if ( a[ i ].first >= 1 && a[ i ].first <= n )
x = a[ i ].first;
else {
if ( a[ i ].first < 1 )
x = 1;
else
x = n;
}
}
auto it = st.lower_bound( { x, y } );
auto itt = st.upper_bound( { x, y } );
if ( itt != st.begin() )
itt = --itt;
if ( it != st.end() && d( a[ i ].first, a[ i ].second, it->first, it->second ) < d( a[ i ].first, a[ i ].second, itt->first, itt->second ) ) {
st.erase( it );
ans += d( a[ i ].first, a[ i ].second, it->first, it->second );
}
else {
st.erase( itt );
ans += d( a[ i ].first, a[ i ].second, it->first, it->second );
}
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |