/**
* 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;
int a[N], cnt[N][3];
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];
}
multiset<pair<int, int>> coins;
for (int i = 1; i <= n; i++) {
if (cnt[i][1] > 1) {
for (int rep = 0; rep < cnt[i][1] - 1; rep++)
coins.insert({i, 1});
}
if (cnt[i][2] > 1) {
for (int rep = 0; rep < cnt[i][2] - 1; rep++)
coins.insert({i, 2});
}
}
auto coins1 = coins;
int ans1 = 0, ans2 = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= 2; j++) {
if (!cnt[i][j]) {
auto it = coins.begin();
if (it != coins.end()) {
int x = it->first, y = it->second;
ans1 += (abs(x - i) + abs(y - j));
coins.erase(it);
}
if (!sz(coins))
break;
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= 2; j++) {
if (!cnt[i][j]) {
auto it = coins1.end();
it--;
int x = it->first, y = it->second;
ans2 += (abs(x - i) + abs(y - j));
coins1.erase(it);
if (!sz(coins1))
break;
}
}
}
cout << ans + min(ans1, ans2);
}
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:174:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
174 | freopen((f + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t4.cpp:175:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
175 | freopen((f + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |