Submission #1208079

#TimeUsernameProblemLanguageResultExecution timeMemory
1208079zsomborCoin Collecting (JOI19_ho_t4)C++20
100 / 100
30 ms3572 KiB
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
using ll = long long;

ll n, x, y, a, b, ans;
vector <ll> A(2e5, 0);
vector <ll> B(2e5, 0);

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n;
	for (int i = 0; i < 2 * n; i++) {
		cin >> x >> y;
		if (x < 1) { ans += 1 - x; x = 1; }
		if (x > n) { ans += x - n; x = n; }
		if (y <= 1) { ans += 1 - y; A[x]++; }
		else { ans += y - 2; B[x]++; }
	}
	for (int i = 1; i <= n; i++) {
		a += A[i] - 1;
		b += B[i] - 1;
		if (a > 0 && b < 0) {
			int k = min(a, -b);
			a -= k;
			b += k;
			ans += k;
		}
		if (b > 0 && a < 0) {
			int k = min(b, -a);
			b -= k;
			a += k;
			ans += k;
		}
		ans += abs(a);
		ans += abs(b);
	}
	cout << ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...