#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define x first
#define y second
#define all(a) (a).begin(), (a).end()
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
ll ans = 0;
vector<pair<int, int>> v;
for (int i = 0; i < 2 * n; i++) {
int x, y;
cin >> x >> y;
x--;
y--;
if (x < 0) {
ans += -x;
x = 0;
}
if (x > n - 1) {
ans += x - n + 1;
x = n - 1;
}
if (y < 0) {
ans += -y;
y = 0;
}
if (y > 1) {
ans += y - 1;
y = 1;
}
v.push_back({x, y});
}
sort(v.begin(), v.end());
ll mn = 1e18;
for (int mask = 0; mask < (1 << (2 * n)); mask++) {
vector<int> a, b;
for (int i = 0; i < 2 * n; i++) {
int y = v[i].y ^ ((mask >> i) & 1);
if (y) {
a.push_back(v[i].x);
} else {
b.push_back(v[i].x);
}
}
if (a.size() != n) continue;
ll s = __builtin_popcount(mask);
for (int i = 0; i < n; i++) {
s += abs(a[i] - i);
s += abs(b[i] - i);
}
mn = min(mn, s);
}
ans += mn;
cout << ans << endl;
return 0;
}
Compilation message
joi2019_ho_t4.cpp: In function 'int main()':
joi2019_ho_t4.cpp:57:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
57 | if (a.size() != n) continue;
| ~~~~~~~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
450 ms |
364 KB |
Output is correct |
5 |
Correct |
475 ms |
364 KB |
Output is correct |
6 |
Correct |
490 ms |
388 KB |
Output is correct |
7 |
Correct |
444 ms |
492 KB |
Output is correct |
8 |
Correct |
451 ms |
640 KB |
Output is correct |
9 |
Correct |
452 ms |
492 KB |
Output is correct |
10 |
Correct |
445 ms |
492 KB |
Output is correct |
11 |
Correct |
441 ms |
492 KB |
Output is correct |
12 |
Correct |
474 ms |
392 KB |
Output is correct |
13 |
Correct |
445 ms |
364 KB |
Output is correct |
14 |
Correct |
457 ms |
492 KB |
Output is correct |
15 |
Correct |
448 ms |
492 KB |
Output is correct |
16 |
Correct |
457 ms |
392 KB |
Output is correct |
17 |
Correct |
446 ms |
392 KB |
Output is correct |
18 |
Correct |
467 ms |
364 KB |
Output is correct |
19 |
Correct |
443 ms |
364 KB |
Output is correct |
20 |
Correct |
446 ms |
364 KB |
Output is correct |
21 |
Correct |
441 ms |
364 KB |
Output is correct |
22 |
Correct |
436 ms |
492 KB |
Output is correct |
23 |
Correct |
457 ms |
364 KB |
Output is correct |
24 |
Correct |
450 ms |
364 KB |
Output is correct |
25 |
Correct |
442 ms |
392 KB |
Output is correct |
26 |
Correct |
457 ms |
492 KB |
Output is correct |
27 |
Correct |
441 ms |
392 KB |
Output is correct |
28 |
Correct |
447 ms |
392 KB |
Output is correct |
29 |
Correct |
446 ms |
492 KB |
Output is correct |
30 |
Correct |
489 ms |
364 KB |
Output is correct |
31 |
Correct |
440 ms |
492 KB |
Output is correct |
32 |
Correct |
447 ms |
492 KB |
Output is correct |
33 |
Correct |
439 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
450 ms |
364 KB |
Output is correct |
5 |
Correct |
475 ms |
364 KB |
Output is correct |
6 |
Correct |
490 ms |
388 KB |
Output is correct |
7 |
Correct |
444 ms |
492 KB |
Output is correct |
8 |
Correct |
451 ms |
640 KB |
Output is correct |
9 |
Correct |
452 ms |
492 KB |
Output is correct |
10 |
Correct |
445 ms |
492 KB |
Output is correct |
11 |
Correct |
441 ms |
492 KB |
Output is correct |
12 |
Correct |
474 ms |
392 KB |
Output is correct |
13 |
Correct |
445 ms |
364 KB |
Output is correct |
14 |
Correct |
457 ms |
492 KB |
Output is correct |
15 |
Correct |
448 ms |
492 KB |
Output is correct |
16 |
Correct |
457 ms |
392 KB |
Output is correct |
17 |
Correct |
446 ms |
392 KB |
Output is correct |
18 |
Correct |
467 ms |
364 KB |
Output is correct |
19 |
Correct |
443 ms |
364 KB |
Output is correct |
20 |
Correct |
446 ms |
364 KB |
Output is correct |
21 |
Correct |
441 ms |
364 KB |
Output is correct |
22 |
Correct |
436 ms |
492 KB |
Output is correct |
23 |
Correct |
457 ms |
364 KB |
Output is correct |
24 |
Correct |
450 ms |
364 KB |
Output is correct |
25 |
Correct |
442 ms |
392 KB |
Output is correct |
26 |
Correct |
457 ms |
492 KB |
Output is correct |
27 |
Correct |
441 ms |
392 KB |
Output is correct |
28 |
Correct |
447 ms |
392 KB |
Output is correct |
29 |
Correct |
446 ms |
492 KB |
Output is correct |
30 |
Correct |
489 ms |
364 KB |
Output is correct |
31 |
Correct |
440 ms |
492 KB |
Output is correct |
32 |
Correct |
447 ms |
492 KB |
Output is correct |
33 |
Correct |
439 ms |
364 KB |
Output is correct |
34 |
Correct |
614 ms |
492 KB |
Output is correct |
35 |
Incorrect |
577 ms |
492 KB |
Output isn't correct |
36 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
450 ms |
364 KB |
Output is correct |
5 |
Correct |
475 ms |
364 KB |
Output is correct |
6 |
Correct |
490 ms |
388 KB |
Output is correct |
7 |
Correct |
444 ms |
492 KB |
Output is correct |
8 |
Correct |
451 ms |
640 KB |
Output is correct |
9 |
Correct |
452 ms |
492 KB |
Output is correct |
10 |
Correct |
445 ms |
492 KB |
Output is correct |
11 |
Correct |
441 ms |
492 KB |
Output is correct |
12 |
Correct |
474 ms |
392 KB |
Output is correct |
13 |
Correct |
445 ms |
364 KB |
Output is correct |
14 |
Correct |
457 ms |
492 KB |
Output is correct |
15 |
Correct |
448 ms |
492 KB |
Output is correct |
16 |
Correct |
457 ms |
392 KB |
Output is correct |
17 |
Correct |
446 ms |
392 KB |
Output is correct |
18 |
Correct |
467 ms |
364 KB |
Output is correct |
19 |
Correct |
443 ms |
364 KB |
Output is correct |
20 |
Correct |
446 ms |
364 KB |
Output is correct |
21 |
Correct |
441 ms |
364 KB |
Output is correct |
22 |
Correct |
436 ms |
492 KB |
Output is correct |
23 |
Correct |
457 ms |
364 KB |
Output is correct |
24 |
Correct |
450 ms |
364 KB |
Output is correct |
25 |
Correct |
442 ms |
392 KB |
Output is correct |
26 |
Correct |
457 ms |
492 KB |
Output is correct |
27 |
Correct |
441 ms |
392 KB |
Output is correct |
28 |
Correct |
447 ms |
392 KB |
Output is correct |
29 |
Correct |
446 ms |
492 KB |
Output is correct |
30 |
Correct |
489 ms |
364 KB |
Output is correct |
31 |
Correct |
440 ms |
492 KB |
Output is correct |
32 |
Correct |
447 ms |
492 KB |
Output is correct |
33 |
Correct |
439 ms |
364 KB |
Output is correct |
34 |
Correct |
614 ms |
492 KB |
Output is correct |
35 |
Incorrect |
577 ms |
492 KB |
Output isn't correct |
36 |
Halted |
0 ms |
0 KB |
- |