#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]++;
}
//cout<<ans<<endl;
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 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 time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |