Submission #1180675

#TimeUsernameProblemLanguageResultExecution timeMemory
1180675miniobCoin Collecting (JOI19_ho_t4)C++20
100 / 100
114 ms1332 KiB
#include <bits/stdc++.h>
using namespace std;

int tab[100007][2];

int main() 
{
	int n;
	long long odp = 0;
	cin >> n;
	for(int i = 0; i < 2 * n; i++)
	{
		int x, y;
		cin >> x >> y;
		if(x > n)
		{
			odp += x - n;
			x = n;
		}
		if(x < 1)
		{
			odp += abs(x - 1);
			x = 1;
		}
		if(y > 2)
		{
			odp += y - 2;
			y = 2;
		}
		if(y < 1)
		{
			odp += abs(y - 1);
			y = 1;
		}
		tab[x][y - 1]++;
	}
	for(int i = 1; i <= n; i++)
	{
		tab[i][0]--;
		tab[i][1]--;
		if(tab[i][0] < 0 && tab[i][1] > 0)
		{
			int ile = min(abs(tab[i][0]), tab[i][1]);
			odp += ile;
			tab[i][0] += ile;
			tab[i][1] -= ile;
		}
		else if(tab[i][0] > 0 && tab[i][1] < 0)
		{
			int ile = min(tab[i][0], abs(tab[i][1]));
			odp += ile;
			tab[i][0] -= ile;
			tab[i][1] += ile;
		}
		tab[i + 1][0] += tab[i][0];
		tab[i + 1][1] += tab[i][1];
		odp += abs(tab[i][0]);
		odp += abs(tab[i][1]);
	}
	cout << odp << endl;
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...