#include "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;//1 -> n, 1 -> 2
cin >> n;
n *= 2;
vector<int> x(n), y(n);
for (int i = 0; i < n; ++i) {
cin >> x[i] >> y[i];
}
long long ans = 0;
vector<vector<int>> p(n / 2 + 1, vector<int> (3));
for (int i = 0; i < n; ++i) {
int nxtx = 0, nxty = 0;
if (1 <= x[i] && x[i] <= n / 2) {
nxtx = x[i];
} else if (x[i] < 1) {
nxtx = 1;
} else {
nxtx = n / 2;
}
if (y[i] <= 1) {
nxty = 1;
} else {
nxty = 2;
}
ans += abs(x[i] - nxtx);
ans += abs(y[i] - nxty);
p[nxtx][nxty]++;
}
vector<pair<int, int>> f;
vector<pair<int, int>> t;
for (int i = 1; i <= n / 2; ++i) {
for (int j = 1; j <= 2; ++j) {
if (p[i][j] > 1) {
for (int k = 1; k < p[i][j]; ++k) {
f.emplace_back(i, j);
}
} else if (p[i][j] == 0) {
t.emplace_back(i, j);
}
}
}
sort(f.begin(), f.end(), [&](pair<int, int> i, pair<int, int> j) {
return i.first + i.second < j.first + j.second;
});
sort(t.begin(), t.end(), [&](pair<int, int> i, pair<int, int> j) {
return i.first + i.second < j.first + j.second;
});
assert(t.size() == f.size());
for (int i = 0; i < (int) t.size(); ++i) {
ans += abs(t[i].first - f[i].first);
ans += abs(t[i].second - f[i].second);
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |