// File coincollecting.cpp created on 02.10.2025 at 11:58:40
#include <bits/stdc++.h>
using i64 = long long;
#ifdef DEBUG
#include "/home/ahmetalp/Desktop/Workplace/debug.h"
#else
#define debug(...) void(23)
#endif
constexpr i64 inf = i64(1E18);
template<typename T>
bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int N;
std::cin >> N;
std::vector<std::array<int, 2>> A(N * 2);
for (int i = 0; i < N * 2; ++i) {
std::cin >> A[i][0] >> A[i][1];
--A[i][0], --A[i][1];
}
i64 ans = 0;
for (int i = 0; i < N * 2; ++i) {
int x, y;
if (A[i][0] < 0) {
x = 0;
} else if (A[i][0] < N) {
x = A[i][0];
} else {
x = N - 1;
}
if (A[i][1] <= 0) {
y = 0;
} else {
y = 1;
}
ans += std::abs(x - A[i][0]) + std::abs(y - A[i][1]);
A[i][0] = x;
A[i][1] = y;
debug(x, y);
}
std::vector<std::vector<int>> cnt(2, std::vector<int>(N));
for (int i = 0; i < N * 2; ++i) {
cnt[A[i][1]][A[i][0]]++;
}
int d0 = 0, d1 = 0;
for (int i = 0; i < N; ++i) {
d0 += cnt[0][i] - 1;
d1 += cnt[1][i] - 1;
if (d0 > 0 && 0 > d1) {
int x = std::min(d0, -d1);
d0 -= x;
d1 += x;
ans += x;
}
if (d1 > 0 && 0 > d0) {
int x = std::min(d1, -d0);
d1 -= x;
d0 += x;
ans += x;
}
ans += std::abs(d0) + std::abs(d1);
}
std::cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |