//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;
void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << "'" << x << "'";}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#define int long long
typedef pair<int, int> T;
const int oo = 1e18, oo2 = 1e9+7, K = 30;
const int mod = 998244353;
void solve(){
int n; cin >> n;
int N = 2*n;
int ans = 0;
vector<T>a(N);
vector cnt(n+2, vector<int>(3));
for (auto &[x, y] : a){
cin >> x >> y;
if (x < 1) {
ans += (1-x);
x = 1;
}
if (x > n){
ans += (x - n);
x = n;
}
if (y < 1){
ans += (1 - y);
y = 1;
}
if (y > 2){
ans += (y - 2);
y = 2;
}
cnt[x][y-1]++;
}
debug(ans);
// for (int j = 1; j>=0; j--){
// for (int i = 1; i<=n; i++){
// cerr << cnt[i][j] << " ";
// }
// cerr << "\n";
// }
vector<int>leave(2), mv(2);
for (int what = 0; what < 2; what++){
vector<int>ord;
int sgn;
if (!what) {
for (int i = 1; i<=n; i++) ord.emplace_back(i);
sgn = 1;
} else {
for (int i = n; i>=1; i--) ord.emplace_back(i);
sgn = -1;
}
for (auto i: ord){
debug(i, ans, cnt[i][0], cnt[i][1]);
int d = cnt[i][0] + cnt[i][1];
if (d < 2){
//musimy znalezc cos zeby przyciagnac
for (int rep = 0; rep < 2; rep++){
if (!cnt[i][rep]){
leave[rep]++;
}
}
continue;
}
//d >= 2
mv[0] = 0; mv[1] = 0;
for (int rep = 0; rep < 2; rep++){
int t = min(leave[rep], max(0ll, cnt[i][rep]-1)); //t zostaje w miejscu, przesuwamy je pozniej w lewo na tym samym lvl
leave[rep] -= t;
ans += t;
cnt[i-sgn][rep] += t;
cnt[i][rep] -= t;
mv[rep] = max(0ll, cnt[i][rep]-1);
}
debug(cnt[i][0], cnt[i][1]);
debug(mv);
for (int rep = 0; rep < 2; rep++){
if (leave[rep]){
int t = min(mv[rep^1], leave[rep]);
//tyle mozemy ukrasc i przesunac w pionie, by potem miec na lewo
ans += 2*t;
cnt[i-sgn][rep] += t;
cnt[i][rep^1] -= t;
leave[rep] -= t;
}
}
d = cnt[i][0] + cnt[i][1];
if (d < 2){
for (int rep = 0; rep < 2; rep++){
if (!cnt[i][rep]){
leave[rep]++;
}
}
continue;
}
for (int rep = 0; rep < 2; rep++){
if (!cnt[i][rep] && mv[rep^1]){
ans++;
cnt[i][rep]++;
cnt[i][rep^1]--;
}
}
for (int rep = 0; rep < 2; rep++){
cnt[i+sgn][rep] += (cnt[i][rep]-1);
ans += (cnt[i][rep]-1);
cnt[i][rep] = 1;
}
debug(cnt[i][0], cnt[i][1]);
}
// for (int j = 1; j>=0; j--){
// for (int i = 1; i<=n; i++){
// cerr << cnt[i][j] << " ";
// }
// cerr << "\n";
// }
// debug(ans);
// return;
}
cout << ans << "\n";
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
//cin >> t;
while (t--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
340 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
1 ms |
340 KB |
Output is correct |
62 |
Correct |
1 ms |
340 KB |
Output is correct |
63 |
Correct |
1 ms |
340 KB |
Output is correct |
64 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
340 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
1 ms |
340 KB |
Output is correct |
62 |
Correct |
1 ms |
340 KB |
Output is correct |
63 |
Correct |
1 ms |
340 KB |
Output is correct |
64 |
Correct |
1 ms |
340 KB |
Output is correct |
65 |
Correct |
29 ms |
10704 KB |
Output is correct |
66 |
Correct |
41 ms |
15420 KB |
Output is correct |
67 |
Correct |
41 ms |
13868 KB |
Output is correct |
68 |
Correct |
48 ms |
13872 KB |
Output is correct |
69 |
Correct |
50 ms |
13884 KB |
Output is correct |
70 |
Correct |
47 ms |
13884 KB |
Output is correct |
71 |
Correct |
45 ms |
13888 KB |
Output is correct |
72 |
Correct |
37 ms |
13884 KB |
Output is correct |
73 |
Correct |
41 ms |
13880 KB |
Output is correct |
74 |
Correct |
38 ms |
13896 KB |
Output is correct |
75 |
Correct |
49 ms |
13752 KB |
Output is correct |
76 |
Correct |
44 ms |
13872 KB |
Output is correct |
77 |
Correct |
37 ms |
13872 KB |
Output is correct |
78 |
Correct |
35 ms |
13880 KB |
Output is correct |
79 |
Correct |
36 ms |
13884 KB |
Output is correct |
80 |
Correct |
37 ms |
13888 KB |
Output is correct |
81 |
Correct |
36 ms |
13872 KB |
Output is correct |
82 |
Correct |
37 ms |
13888 KB |
Output is correct |
83 |
Correct |
36 ms |
13880 KB |
Output is correct |
84 |
Correct |
41 ms |
14128 KB |
Output is correct |
85 |
Correct |
37 ms |
13872 KB |
Output is correct |
86 |
Correct |
37 ms |
13888 KB |
Output is correct |
87 |
Correct |
38 ms |
13880 KB |
Output is correct |
88 |
Correct |
40 ms |
13876 KB |
Output is correct |
89 |
Correct |
38 ms |
13876 KB |
Output is correct |
90 |
Correct |
39 ms |
13872 KB |
Output is correct |
91 |
Correct |
39 ms |
13872 KB |
Output is correct |
92 |
Correct |
39 ms |
14012 KB |
Output is correct |
93 |
Correct |
40 ms |
14000 KB |
Output is correct |
94 |
Correct |
38 ms |
13764 KB |
Output is correct |
95 |
Correct |
37 ms |
13744 KB |
Output is correct |
96 |
Correct |
45 ms |
13864 KB |
Output is correct |
97 |
Correct |
46 ms |
13892 KB |
Output is correct |
98 |
Correct |
41 ms |
13892 KB |
Output is correct |
99 |
Correct |
37 ms |
13880 KB |
Output is correct |
100 |
Correct |
37 ms |
13892 KB |
Output is correct |
101 |
Correct |
37 ms |
13884 KB |
Output is correct |
102 |
Correct |
42 ms |
13872 KB |
Output is correct |
103 |
Correct |
37 ms |
13884 KB |
Output is correct |