제출 #1326023

#제출 시각아이디문제언어결과실행 시간메모리
1326023tkm_algorithmsCoin Collecting (JOI19_ho_t4)C++20
100 / 100
31 ms1848 KiB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
#define int ll
using P = pair<int, int>;
#define all(x) x.begin(), x.end()
#define rep(i, l, n) for (int i = l; i < (n); ++i)
#define sz(x) (int)x.size()
const char nl = '\n';
const int mod = 998244353;

void solve() {
	int n; cin >> n;
	int res = 0;
	vector<int> p(2*n+1);
	rep(i, 0, 2*n) {
		int x, y; cin >> y >> x;
		if (y < 1)res += 1-y, y = 1;
		if (y > n)res += y-n, y = n;
		if (x < 1)res += 1-x, x = 1;
		if (x > 2)res += x-2, x = 2;
		p[(x-1)*n+y] += 1;
	}
	
	//cout << res << nl;
	
	int d1 = 0, d2 = 0;
	rep(i, 1, n+1) {
		d1 += p[i]-1, d2 += p[i+n]-1;
		if (d1>0&&d2<0) {
			int t = min(d1, -d2);
			d1 -= t, d2 += t,
			res += t;
		}
		if (d1<0&&d2>0) {
			int t = min(-d1, d2);
			d1 += t, d2 -= t,
			res += t;
		}
		res += abs(d1)+abs(d2);
	}
	cout << res << nl;
}

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    solve();
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...