/**
* 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
#define int ll
const int N = 1e6 + 12;
int a[N], cnt[N][5];
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 && 1 <= y && y <= 2) {
cnt[x][y]++;
a[i] = 0;
} else if (1 <= x && x <= n) {
if (y < 1) {
cnt[x][1]++;
a[i] = abs(y - 1);
} else {
cnt[x][2]++;
a[i] = abs(y - 2);
}
} else if (1 <= y && y <= 2) {
if (x < 1) {
cnt[1][y]++;
a[i] = abs(x - 1);
} else {
cnt[n][y]++;
a[i] = abs(x - n);
}
} else {
if (x < 1 && y < 1) {
cnt[1][1]++;
a[i] = abs(x - 1) + abs(y - 1);
} else if (x < 1 && y > 2) {
cnt[1][2]++;
a[i] = abs(x - 1) + abs(y - 2);
} else if (x > n && y < 1) {
cnt[n][1]++;
a[i] = abs(x - n) + abs(y - 1);
} else {
cnt[n][2]++;
a[i] = abs(x - n) + abs(y - 2);
}
}
ans += a[i];
}
set<int> l1, l2;
for (int i = 1; i <= n; i++) {
while (sz(l1) && cnt[i][1] > 1) {
ans += abs((*l1.begin()) - i);
cnt[i][1]--;
l1.erase(l1.begin());
}
while (sz(l2) && cnt[i][2] > 1) {
ans += ((*l2.begin()) - i);
cnt[i][2]--;
l2.erase(l2.begin());
}
while (sz(l2) && cnt[i][1] > 1) {
ans += abs((*l2.begin()) - i) + 1;
cnt[i][1]--;
l2.erase(l2.begin());
}
while (sz(l1) && cnt[i][2] > 1) {
ans += ((*l1.begin()) - i) + 1;
cnt[i][2]--;
l1.erase(l1.begin());
}
if (cnt[i][1] > 1 && cnt[i][2] == 0) {
ans++;
cnt[i][2]++;
cnt[i][1]--;
}
if (cnt[i][2] > 1 && cnt[i][1] == 0) {
ans++;
cnt[i][1]++;
cnt[i][2]--;
}
if (cnt[i][1] > 1) {
ans += (cnt[i][1] - 1);
cnt[i + 1][1] += (cnt[i][1] - 1);
cnt[i][1] = 1;
}
if (cnt[i][2] > 1) {
ans += (cnt[i][2] - 1);
cnt[i + 1][2] += (cnt[i][2] - 1);
cnt[i][2] = 1;
}
if (cnt[i][1] == 0)
l1.insert(i);
if (cnt[i][2] == 0)
l2.insert(i);
}
cout << ans;
}
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:188:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
188 | freopen((f + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t4.cpp:189:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
189 | freopen((f + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Incorrect |
1 ms |
204 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 |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Incorrect |
1 ms |
204 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 |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |