제출 #670512

#제출 시각아이디문제언어결과실행 시간메모리
670512dozerCoin Collecting (JOI19_ho_t4)C++14
100 / 100
62 ms10556 KiB
#include <bits/stdc++.h>
using namespace std;
#define fileio() freopen("input.txt", "r", stdin), freopen("output.txt", "w",  stdout)
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
#define pb push_back
#define st first
#define nd second
#define sp " "
#define endl "\n"
#define pii pair<int, int>
#define N 100005
#define int long long

const int modulo = 1e9 + 7;
int arr[N][3];

int32_t main()
{
	fastio();

	int n;
	cin>>n;
	int ans = 0;
	for (int i = 1; i <= 2 * n; i++)
	{
		int x, y;
		cin>>x>>y;
		if (y < 1)
		{
			ans += 1 - y;
			y = 1;
		}
		else if (y > 2) 
		{
			ans += y - 2;
			y = 2;
		}
		if (x < 1)
		{
			ans += 1 - x;
			x = 1;
		}
		else if (x > n)
		{
			ans += x - n;
			x = n;
		}
		arr[x][y]++;	
	}
	queue<pii> emp[3];
	queue<pii> ful[3];
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= 2; j++)
		{
			if (arr[i][j] == 1) continue;
			if (arr[i][j] == 0)
			{
				pii tmp = {0, 0};
				if (!ful[j].empty())
				{
					tmp = ful[j].front();
					ful[j].pop();
				}
				else
				{
					continue;
				}
				arr[i][j] = 1;
				ans += abs(i - tmp.st) + abs(j - tmp.nd);
			}
			else
			{
				while(arr[i][j] > 1)
				{
					pii tmp = {0, 0};
					if (!emp[j].empty())
					{
						tmp = emp[j].front();
						emp[j].pop();
					}
					else
					{
						break;
					}
					ans += abs(i - tmp.st) + abs(j - tmp.nd);
					arr[i][j]--;
				}
			}
		}
		for (int j = 1; j <= 2; j++)
		{
			if (arr[i][j] == 1) continue;
			if (arr[i][j] == 0)
			{
				pii tmp = {0, 0};
				if (!ful[j].empty())
				{
					tmp = ful[j].front();
					ful[j].pop();
				}
				else if (!ful[3 - j].empty())
				{
					tmp = ful[3 - j].front();
					ful[3 - j].pop();
				}
				else
				{
					emp[j].push({i, j});
					continue;
				}
				ans += abs(i - tmp.st) + abs(j - tmp.nd);
			}
			else
			{
				for (int k = 2; k <= arr[i][j]; k++)
				{
					pii tmp = {0, 0};
					if (!emp[j].empty())
					{
						tmp = emp[j].front();
						emp[j].pop();
					}
					else if (!emp[3 - j].empty())
					{
						tmp = emp[3 - j].front();
						emp[3 - j].pop();
					}
					else
					{
						ful[j].push({i, j});
						continue;
					}
					ans += abs(i - tmp.st) + abs(j - tmp.nd);
				}
			}
		}
	}
	cout<<ans<<endl;
	cerr<<"time taken : "<<(float)clock() / CLOCKS_PER_SEC<<" seconds\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...