Submission #1208063

#TimeUsernameProblemLanguageResultExecution timeMemory
1208063algoproclubCoin Collecting (JOI19_ho_t4)C++20
0 / 100
0 ms328 KiB
// UUID: 08bac386-4b26-40e9-938a-28fbf3c7dc98
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
using ll = long long;

ll n, i, j, ans;
vector <ll> a;
vector <ll> b;

void f1(ll x) {
	if (a[i] > n || a[i] > x && b[j] <= x) ans += abs(b[j++] - x) + 1;
	else ans += abs(a[i++] - x);
}

void f2(ll x) {
	if (b[j] > n || b[j] > x && a[i] <= x) ans += abs(a[i++] - x) + 1;
	else ans += abs(b[j++] - x);
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n;
	ll x, y;
	for (int k = 0; k < 2 * n; k++) {
		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.push_back(x); }
		else { ans += y - 2; b.push_back(x); }
	}
	sort(a.begin(), a.end());
	sort(b.begin(), b.end());
	a.push_back(1e9);
	b.push_back(1e9);
	for (int k = 1; k <= n; k++) {
		if (a[i] < b[j]) { f1(k); f2(k); }
		else { f2(k); f1(k); }
	}
	cout << ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...