This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//bit chass 1
#include <bits/stdc++.h>
#define x first
#define y second
#define el "\n"
#define ll long long
#define pb push_back
#define pll pair <ll, ll>
#define pii pair <int, int>
#define all(x) x.begin(), x.end()
#define lcm(x,y) x * y / __gcd(x, y)
#define ibase ios_base::sync_with_stdio(0), cin.tie(0)
using namespace std;
const int N = 1e5 + 5, inf = 1e9 + 7, M = 2e6, MM = 2e6 + 5, K = 300;
const ll MI = 2e18;
const double P = 3.14;
ll c[3][N];
void solve() {
	ll n;
	ll ans = 0;
	cin >> n;
	for (int i = 1; i <= 2 * n; i++) {
		ll x, y;
		cin >> x >> y;
		if (1 <= x && x <= n && 1 <= y && y <= 2)
			c[y][x]++;
		else if (1 <= x && x <= n) {
			if (y <= 0) 
				c[1][x]++, ans += 1 - y;
			else 
				c[2][x]++, ans += y - 2;
		}
		else if (1 <= y && y <= 2) {
			if (x <= 0) 
				c[y][1]++, ans += 1 - x;
			else  
				c[y][n]++, ans += x - n;
		}
		else {
			if (x <= 0 && y <= 0) 
				c[1][1]++, ans += 1 - y + 1 - x;
			if (x <= 0 && y > 2)
				c[2][1]++, ans += 1 - x + y - 2;
			if (x > n && y <= 0)
				c[1][n]++, ans += x - n + 1 - y;
			if (x > n && y > 2)
				c[2][n]++, ans += x - n + y - 2;
		}
	}
	int a = 0, b = 0;
	for (int i = 1; i <= n; i++) {
		a += c[1][i] - 1;
		b += c[2][i] - 1;
		int s = min(abs(a), abs(b));
		if (a > 0 && b < 0) {
			ans += s;
			b += s;
			a -= s;
		}
		else if (a < 0 && b > 0) {
			ans += s;
			b -= s;
			a += s;
		}
		ans += abs(a) + abs(b);
	}
	cout << ans;
}
int main() {
	ibase;
	int T = 1;
//	cin >> T;
	for (int i = 1; i <= T; i++) {
//		cout << "Case " << i << ": ";
		solve();
		cout << el;
	}
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |