/**
* LES GREATEABLES BRO TEAM
**/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define sz(x) (int)x.size()
const bool FLAG = false;
void setIO(const string &f = "");
string to_string(const string s) {
return '"' + s + '"';
}
string to_string(const char c) {
return char(39) + string(1, c) + char(39);
}
string to_string(const char* s) {
return to_string(string(s));
}
string to_string(bool f) {
return (f ? "true" : "false");
}
template<class A, class B>
string to_string(const pair<A, B> x) {
return "(" + to_string(x.first) + ", " + to_string(x.second) + ")";
}
template<class A, class B, class C>
string to_string(const tuple<A, B, C> x) {
return "(" + to_string(get<0>(x)) + ", " + to_string(get<1>(x)) + ", " + to_string(get<2>(x)) + ")";
}
template<class A, class B, class C, class D>
string to_string(const tuple<A, B, C, D> x) {
return "(" + to_string(get<0>(x)) + ", " + to_string(get<1>(x)) + ", " + to_string(get<2>(x)) + ", " + to_string(get<3>(x)) + ")";
}
template<class T>
string to_string(const T v) {
string res = "{"; bool f = true;
for (auto x: v)
res += (f ? to_string(x) : ", " + to_string(x)), f = false;
return res + "}";
}
void debug_args() { cerr << "]\n"; }
template<class H, class... T>
void debug_args(H x, T... y) {
cerr << to_string(x);
if (sizeof... (y))
cerr << ", ";
debug_args(y...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: [", debug_args(__VA_ARGS__);
#else
#define debug(...) 47
#endif
const int N = 3e5 + 12;
#define int ll
int a[N], cnt[N][3];
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void solve() {
int n;
cin >> n;
ll ans = 0;
for (int i = 0; i < n + n; i++) {
int x, y;
cin >> x >> y;
if (1 <= x && x <= n) {
if (y <= 1) {
a[i] = abs(y - 1);
cnt[x][1]++;
} else {
a[i] = abs(y - 2);
cnt[x][2]++;
}
} else if (1 <= y && y <= 2) {
if (x < 1) {
a[i] = abs(x - 1);
cnt[1][y]++;
} else if (1 <= x && x <= n) {
a[i] = 0;
cnt[x][y]++;
} else {
a[i] = abs(x - n);
cnt[n][y]++;
}
} else {
if (x < 1) {
if (y < 1) {
a[i] = abs(x - 1) + abs(y - 1);
cnt[1][1]++;
} else {
a[i] = abs(x - 1) + abs(y - 2);
cnt[1][2]++;
}
} else {
if (y < 1) {
a[i] = abs(x - n) + abs(y - 1);
cnt[n][1]++;
} else {
a[i] = abs(x - n) + abs(y - 2);
cnt[n][2]++;
}
}
}
ans += a[i];
}
vector<pair<int, int>> v1;
vector<pair<int, int>> v2;
for (int i = 1; i <= n; i++) {
if (cnt[i][1] > 1) {
for (int rep = 0; rep < cnt[i][1] - 1; rep++)
v1.push_back({i, 1});
} else if (cnt[i][1] == 0)
v2.push_back({i, 1});
if (cnt[i][2] > 1) {
for (int rep = 0; rep < cnt[i][2] - 1; rep++)
v1.push_back({i, 2});
} else if (cnt[i][2] == 0)
v2.push_back({i, 2});
}
int mn = 1e15 + 1532;
for (int _ = 0; _ < 700; _++) {
shuffle(v1.begin(), v1.end(), rng);
shuffle(v2.begin(), v2.end(), rng);
int cur = 0;
for (int i = 0; i < sz(v1); i++) {
cur += abs(v1[i].first - v2[i].first) + abs(v1[i].second - v2[i].second);
}
mn = min(mn, cur);
}
cout << ans + mn;
}
signed main() {
setIO();
int tt = 1;
if (FLAG) {
cin >> tt;
}
while (tt--) {
solve();
}
return 0;
}
void setIO(const string &f) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (fopen((f + ".in").c_str(), "r")) {
freopen((f + ".in").c_str(), "r", stdin);
freopen((f + ".out").c_str(), "w", stdout);
}
}
Compilation message
joi2019_ho_t4.cpp: In function 'void setIO(const string&)':
joi2019_ho_t4.cpp:162:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
162 | freopen((f + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t4.cpp:163:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
163 | freopen((f + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |