#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector <ll>;
using ii = pair <ll, ll>;
using vii = vector <ii>;
int main () {
cin.tie(nullptr) -> sync_with_stdio(false);
ll n;
cin >> n;
vii ve(2*n);
for (auto &[x, y] : ve) cin >> x >> y;
ll ans = 0;
auto fclamp = [&](ll l, ll r, ll &val) {
if (val < l) { ans += l-val; val = l; }
if (val > r) { ans += val-r; val = r; }
};
for (auto &[x, y] : ve) {
fclamp(1, n, x);
fclamp(1, 2, y);
}
vll asg(2*n);
iota(asg.begin(), asg.end(), 0);
auto cost = [&](ll pos, ll i) {
auto [x, y] = ve[i];
return abs(x-(1+pos%n)) + abs(y-(1+pos/n));
};
auto shsw = [&](ll i, ll j) {
return cost(i, asg[j]) + cost(j, asg[i]) < cost(i, asg[i]) + cost(j, asg[j]);
};
for (ll k = 0; k < 2*n; k++) {
for (ll i = 0; i < 2*n; i++) {
for (ll j = 0; j < 2*n; j++) {
if (shsw(i, j)) swap(asg[i], asg[j]);
}
}
}
for (ll i = 0; i < 2*n; i++) ans += cost(i, asg[i]);
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
- |