#include <bits/stdc++.h>
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define fast ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define all(v) v.begin(),v.end()
#define pb push_back
#define sz size()
#define ft first
#define sd second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef unsigned long long ull;
const int N = 1e6 + 5;
const ll M = 1e8;
const ll inf = 1e18;
const ll mod = 1e9;
const double Pi = acos(-1);
ll binpow(ll x, ll ti) { ll res = 1;while (ti){if(ti & 1)res *= x;x *= x;ti >>= 1; x %= mod; res %= mod;} return res;}
ll binmul(ll x, ll ti) { ll res = 0;while (ti){if(ti & 1)res += x;x += x;ti >>= 1; x %= mod; res %= mod;} return res;}
ll nok(ll a, ll b) { return (a*b)/__gcd(abs(a),abs(b)) * (a*b > 0 ? 1 : -1); }
bool odd(ll n) { return (n % 2 == 1); }
bool even(ll n) { return (n % 2 == 0); }
ll n, x[N], y[N], cnt[N][3], ans1;
bool used[N], used2[N][3];
ll rec1() {
ll d = 0, u = 0, ans = 0;
vector <pll> v;
for (int i = 0; i < n * 2; ++ i) {
if (x[i] >= 1 && x[i] <= n && y[i] >= 1 && y[i] <= 2 && !used2[x[i]][y[i]]) {
if (y[i] == 1) ++ d;
else ++ u;
used2[x[i]][y[i]] = 1;
continue;
}
else v.pb({y[i], x[i]});
}
sort(all(v));
vector <pll> a, b;
for (int i = 0; i < v.sz; ++ i) {
if (i < n - d) a.pb({v[i].sd, v[i].ft});
else b.pb({v[i].sd, v[i].ft});
}
sort(all(a));
sort(all(b));
pll dd = {1, 1};
for (auto [x, y] : a) {
while (used2[dd.ft][dd.sd]) ++ dd.ft;
ans += abs(x - dd.ft) + abs(y - dd.sd);
used2[dd.ft][dd.sd] = 1;
++ dd.ft;
}
pll uu = {1, 2};
for (auto [x, y] : b) {
while(used2[uu.ft][uu.sd]) ++ uu.ft;
ans += abs(x - uu.ft) + abs(y - uu.sd);
used2[uu.ft][uu.sd] = 1;
++ uu.ft;
}
return ans;
}
const void solve(/*Armashka*/) {
cin >> n;
for (int i = 1; i <= n * 2; ++ i) {
cin >> x[i] >> y[i];
if (x[i] >= 1 && x[i] <= n && y[i] >= 1 && y[i] <= 2) ++ cnt[x[i]][y[i]];
}
for (int i = 1; i <= n; ++ i) {
for (int j = 1; j <= 2; ++ j) {
if (cnt[i][j]) continue;
ll cur = -1, mn = inf;
for (int k = 1; k <= n * 2; ++ k) {
if (used[k]) continue;
ll res = abs(i - x[k]) + abs(j - y[k]);
if (x[k] >= 1 && x[k] <= n && y[k] >= 1 && y[k] <= 2) {
if (cnt[x[k]][y[k]] > 1 && res < mn) mn = res, cur = k;
} else if (res < mn) mn = res, cur = k;
}
//cout << i << " " << j << " = " << x[cur] << " " << y[cur] << "\n";
used[cur] = 1;
ans1 += mn;
if (x[cur] >= 1 && x[cur] <= n && y[cur] >= 1 && y[cur] <= 2) -- cnt[x[cur]][y[cur]];
}
}
cout << min(ans1, rec1()) << "\n";
}
signed main() {
fast;
//freopen("divide.in", "r", stdin);
//freopen("divide.out", "w", stdout);
int tt = 1;
//cin >> tt;
while (tt --) {
solve();
}
}
/*
5 4 4
1 2
3 1
3 4
5 3
4 5 2 3
2 1 3 1
1 3 5
2 3 4 5
2 1 3 1
*/
Compilation message
joi2019_ho_t4.cpp: In function 'll rec1()':
joi2019_ho_t4.cpp:48:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for (int i = 0; i < v.sz; ++ i) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |