제출 #595447

#제출 시각아이디문제언어결과실행 시간메모리
595447AsymmetryCoin Collecting (JOI19_ho_t4)C++17
100 / 100
78 ms10444 KiB
#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#define FOR(i,l,r) for(int i=(l);i<=(r);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) int(x.size())
template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';}
template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';}
#ifdef DEBUG
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x)
#else
#define debug(...) {}
#endif

int main() {
	cin.tie(0)->sync_with_stdio(0);
	int n;
	cin >> n;
	LL odp = 0;
	vector<vector<int>> t(n, vector (2, -1));
	REP (i, n * 2) {
		int a, b;
		cin >> a >> b;
		if (b > 1) {
			odp += b - 2;
			b = 1;
		}
		else {
			odp += 1 - b;
			b = 0;
		}
		if (a < 1) {
			odp += 1 - a;
			a = 1;
		}
		if (a > n) {
			odp += a - n;
			a = n;
		}
		--a;
		++t[a][b];
	}
	int a = 0, b = 0;
	REP (i, n) {
		a += t[i][0];
		b += t[i][1];
		if (a < 0 && b > 0) {
			int g = min(-a, b);
			odp += g;
			a += g;
			b -= g;
		}
		if (b < 0 && a > 0) {
			int g = min(a, -b);
			odp += g;
			a -= g;
			b += g;
		}
		odp += abs(a) + abs(b);
	}
	cout << odp << endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...