Submission #890882

#TimeUsernameProblemLanguageResultExecution timeMemory
890882vjudge1Coin Collecting (JOI19_ho_t4)C++17
100 / 100
36 ms7932 KiB
//order_of_key(k): Number of items strictly smaller than k .
//find_by_order(k): K-th element in a set (counting from zero).

//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define file(s) if(fopen(s".in","r")) freopen(s".in","r",stdin);freopen(s".out","w",stdout)
#define all(x) (x).begin(), (x).end()
#define len(x) (int)x.size()
#define tm (tl + tr >> 1)
#define ls v << 1, tl, tm
#define rs v << 1 | 1, tm + 1, tr
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define elif else if
#define F first
#define S second
#define int long long

using namespace std;
using namespace __gnu_pbds;

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef unsigned long long ull;
typedef long long ll;
typedef double db;
typedef long double ld;

const int MOD = 1e9 + 7;
const int N = 2e5 + 7;
const int P = 911;
const ll INF = 1e18;

int rnd() {
	int x = rand() << 15;
	return x ^ rand();
}

int cnt[N][3];

void GazizMadi() {
	int n, ans = 0;
	cin >> n;
	for (int i = 1; i <= (n << 1); i++) {
		int x, y;
		cin >> x >> y;
		if (x > n) {
			ans += x - n;
			x = n;
		}
		if (x < 1) {
			ans += 1 - x;
			x = 1;
		}
		if (y > 2) {
			ans += y - 2;
			y = 2;
		}
		if (y < 1) {
			ans += 1 - y;
			y = 1;
		}
		cnt[x][y]++;
	}
	int x = 0, y = 0;
	// cout << ans << '\n';
	for (int i = 1; i <= n; i++) {
		x += cnt[i][1] - 1;
		y += cnt[i][2] - 1;
		while (x > 0 && y < 0) {
			x--;
			y++;
			ans++;
		}
		while (x < 0 && y > 0) {
			x++;
			y--;
			ans++;
		}
		ans += abs(x) + abs(y);
	}
	cout << ans;
}

const bool Cases = 0;

signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	srand(time(0));
	int TT = 1;
	if (Cases) cin >> TT;
	for (int i = 1; i <= TT; i++) {
		//cout << "Case " << i << ": ";
		GazizMadi();
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...