// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
#define sz(x) int(x.size())
using ll = long long;
template<class T> using V = vector<T>;
int main() {
cin.tie(0)->sync_with_stdio(0);
int N; cin >> N;
V<V<int>> A(2, V<int>(N, 0));
ll ans = 0;
for(int t = 0; t < 2*N; t++) {
int x, y; cin >> x >> y;
int r = (y >= 2 ? 1 : 0);
int c = min(max(1, x), N) - 1;
ans += abs((r + 1) - y);
ans += abs((c + 1) - x);
// cout << r << " " << c << endl;
A[r][c]++;
}
// for(int r = 0; r < 2; r++) {
// for(int c = 0; c < N; c++) cout << A[r][c] << " ";
// cout << nl;
// }
// cout << ans << nl;
V<queue<array<int, 2>>> E(2), F(2); // EMPTY or FILLED
for(int i = 0; i < N; i++) {
for(int r = 0; r < 2; r++) {
if (A[r][i] > 1) {
while(A[r][i] > 1 && sz(E[r])) {
auto [pr, pc] = E[r].front(); E[r].pop();
ans += abs(r - pr) + abs(pc - i);
--A[r][i];
}
while(A[r][i] > 1 && sz(E[r^1])) {
auto [pr, pc] = E[r^1].front(); E[r^1].pop();
ans += abs(r - pr) + abs(pc - i);
--A[r][i];
}
}
if (A[r][i] < 1) {
if (sz(F[r])) {
auto [pr, pc] = F[r].front(); F[r].pop();
ans += abs(r - pr) + abs(pc - i);
A[r][i] = 1;
} else if (sz(F[r^1])) {
auto [pr, pc] = F[r^1].front(); F[r^1].pop();
ans += abs(r - pr) + abs(pc - i);
A[r][i] = 1;
}
}
for(int t = 0; t < abs(1 - A[r][i]); t++) {
if (A[r][i] < 1) E[r].push({r, i});
else F[r].push({r, i});
}
}
}
cout << ans << nl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Incorrect |
1 ms |
316 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Incorrect |
1 ms |
316 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Incorrect |
1 ms |
316 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |