#include <bits/stdc++.h>
#define respagold ios_base::sync_with_stdio(0), cin.tie(0);
#define ll long long
#define int long long
#define int2 __int128_t
#define FOR( i, x, n, d ) for( int i = x; i <= n; i += d )
#define FORR( i, x, n, d ) for( int i = x; i >= n; i -= d )
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x.size())
#define pb push_back
#define ins insert
#define lb lower_bound
#define ub upper_bound
#define pii pair <int, int>
#define mkp make_pair
using namespace std;
const int N = 1e6 + 123;
const int inf = 2e18;
const int MOD = 1e9 + 7;
const int MOD1 = 998244353;
const int P = 6547;
int a[N], b[N], c[N], n, m, x, y, z, w;
int mz[N], used[N][2];
mt19937 rng( chrono::steady_clock::now().time_since_epoch().count());
int rand( int l, int r )
{
uniform_int_distribution <int> uid( l, r );
return uid( rng );
}
void solve()
{
cin >> n;
int ans = 0;
FOR( i, 1, n * 2, 1 )
{
cin >> x >> y;
if( x < 1 ) ans += 1 - x, x = 1;
if( x > n ) ans += x - n, x = n;
if( y < 1 ) ans += 1 - y, y = 1;
if( y > 2 ) ans += y - 2, y = 2;
used[x][y] ++;
}
FORR( i, n, 1, 1 )
{
FOR( j, 1, 2, 1 )
{
if( !used[i][j] )
{
int mn = inf, idx = -1, idy = -1;
FORR( i2, n, i, 1 )
{
int f = 1, s = 2;
if( j == 2 ) swap(f, s);
if( used[i2][f] > 1 )
{
idx = i2, idy = f;
break;
}
if( used[i2][s] > 1 )
{
idx = i2, idy = s;
break;
}
}
if( idy == -1 )
{
FORR( i2, i - 1, 1, 1 )
{
int f = 1, s = 2;
if( j == 2 ) swap(f, s);
if( used[i2][f] > 0 )
{
idx = i2, idy = f;
break;
}
if( used[i2][s] > 0 )
{
idx = i2, idy = s;
break;
}
}
}
used[idx][idy] --;
used[i][j] = 1;
ans += abs(i - idx) + abs(j - idy);
}
}
}
cout << ans;
}
signed main()
{
// freopen("connect.in", "r", stdin);
// freopen("connect.out", "w", stdout);
respagold
int test = 1;
if( !test ) cin >> test;
while( test -- )
{
solve();
}
}
// solved by KluydQ
Compilation message (stderr)
joi2019_ho_t4.cpp: In function 'void solve()':
joi2019_ho_t4.cpp:58:39: warning: iteration 1 invokes undefined behavior [-Waggressive-loop-optimizations]
58 | if( !used[i][j] )
| ~~~~~~~~~^
joi2019_ho_t4.cpp:7:45: note: within this loop
7 | #define FOR( i, x, n, d ) for( int i = x; i <= n; i += d )
| ^
joi2019_ho_t4.cpp:56:17: note: in expansion of macro 'FOR'
56 | FOR( j, 1, 2, 1 )
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |