This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
#define IO ios::sync_with_stdio(0), cin.tie(0)
#define FOR(i, a, b) for (int i = a, I = b; i <= I; i++)
using namespace std;
using pii = pair<int, int>;
void dbg() {;}
template<class T, class ...U>
void dbg(T a, U ...b) { cout << a << " "; dbg(b...); }
void ent() { cout << "\n"; }
const int N = 200005;
pii a[N];
int mp[N][3];
signed main() {
IO;
int n;
cin >> n;
FOR(i, 1, 2 * n) cin >> a[i].first >> a[i].second;
int ans = 0;
FOR(i, 1, 2 * n) {
auto [x, y] = a[i];
int tarx, tary;
if (x <= 1) tarx = 1;
else if (x >= n) tarx = n;
else tarx = x;
if (y <= 1) tary = 1;
else tary = 2;
ans += abs(tarx - x) + abs(tary - y);
mp[tarx][tary] += 1;
}
int U_cnt = 0;
int D_cnt = 0;
FOR(i, 1, n) {
if (mp[i][1] == 0)
D_cnt += 1;
if (mp[i][2] == 0)
U_cnt += 1;
if (mp[i][1] > 1) {
if (D_cnt > 0) {
int dif = min(mp[i][1] - 1, D_cnt);
D_cnt -= dif;
mp[i][1] -= dif;
}
}
if (mp[i][2] > 1) {
if (U_cnt > 0) {
int dif = min(mp[i][2] - 1, U_cnt);
U_cnt -= dif;
mp[i][2] -= dif;
}
}
if (mp[i][1] > 1) {
if (U_cnt > 0) {
int dif = min(mp[i][1] - 1, U_cnt);
U_cnt -= dif;
mp[i][1] -= dif;
ans += dif;
}
}
if (mp[i][2] > 1) {
if (D_cnt > 0) {
int dif = min(mp[i][2] - 1, D_cnt);
D_cnt -= dif;
mp[i][2] -= dif;
ans += dif;
}
}
if (mp[i][1] > 1) {
ans += mp[i][1] - 1;
mp[i + 1][1] += mp[i][1] - 1;
mp[i][1] = 1;
}
if (mp[i][2] > 1) {
ans += mp[i][2] - 1;
mp[i + 1][2] += mp[i][2] - 1;
mp[i][2] = 1;
}
ans += U_cnt + D_cnt;
}
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... |