#include <bits/stdc++.h>
#define X first
#define Y second
typedef long long int ll;
ll N, cost = 0;
using namespace std;
ll _x(ll n)
{
return n >= N ? N
: n <= 1 ? 1
: n;
}
ll _cx(ll n)
{
return n >= N ? n - N
: n <= 1 ? 1 - n
: 0;
}
ll _y(ll n)
{
return n <= 1 ? 1 : 2;
}
ll _cy(ll n)
{
return n <= 1 ? 1 - n : n - 2;
}
ll moveX[4] = {-1, 1, 0, 0};
ll moveY[4] = {0, 0, -1, 1};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
vector<pair<ll, ll>> coins;
cin >> N;
vector<vector<ll>> grid(N + 1, vector<ll>(2 + 1, 0));
for (ll i = 0; i < 2 * N; i++)
{
ll x, y;
cin >> x >> y;
grid[_x(x)][_y(y)]++;
cost += _cx(x) + _cy(y);
}
for (ll i = 1; i <= N; i++)
{
for (ll j = 1; j <= 2; j++)
{
if (grid[i][j] == 0)
{
queue<pair<ll, ll>> todo;
todo.push({i, j});
while (!todo.empty())
{
pair<ll, ll> front = todo.front();
todo.pop();
if (front.X < 1 || front.X > N || front.Y < 1 || front.Y > 2)
continue;
if (grid[front.X][front.Y] > 1)
{
grid[front.X][front.Y]--;
grid[i][j]++;
cost += abs(front.X - i) + abs(front.Y - j);
break;
}
for (ll p = 0; p < 4; p++)
{
todo.push({front.X + moveX[p], front.Y + moveY[p]});
}
}
// cout << cost << "\n";
// for (ll i = 1; i <= N; i++)
// {
// for (ll j = 1; j <= 2; j++)
// {
// cout << grid[i][j] << " ";
// }
// cout << "\n";
// }
// cout << "\n";
}
}
}
cout << cost;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |