Submission #430903

#TimeUsernameProblemLanguageResultExecution timeMemory
430903BertedCoin Collecting (JOI19_ho_t4)C++14
100 / 100
78 ms4912 KiB
#include <iostream>
#define ll long long

using namespace std;

int N, A[2][100001];
ll cost = 0;

int main()
{
	ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> N;
	for (int i = 0; i < 2 * N; i++)
	{
		int x, y; cin >> x >> y; x--; y--;
		if (y <= 0)
		{
			cost += -y;
			if (x <= 0) {A[0][0]++; cost += -x;}
			else if (x < N) {A[0][x]++;}
			else {A[0][N - 1]++; cost += x - N + 1;}
		}
		else
		{
			cost += y - 1;
			if (x <= 0) {A[1][0]++; cost += -x;}
			else if (x < N) {A[1][x]++;}
			else {A[1][N - 1]++; cost += x - N + 1;}
		}
	}

	int a = 0, b = 0;
	for (int i = 0; i < N; i++)
	{
		a += A[0][i] - 1; b += A[1][i] - 1;
		//cerr << a << " " << b << "\n";
		while (a > 0 && b < 0) {a--; b++; cost++;}
		while (a < 0 && b > 0) {a++; b--; cost++;}
		cost += abs(a) + abs(b);
	}

	cout << cost << "\n";
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...