//UNSTOPPABLE
#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#define ll long long
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define int long long
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define pii pair<int,int>
#define tpii pair <pair <int,int> , int>
#define bruh cout << "NO\n"
using namespace std;
using namespace __gnu_pbds;
const int N = 3e5 + 5;
int mod = 1e9 + 7;
const int INF = 1e18;
int n,a[N],b[N],p[N][3];
int dist(int a , int b , int c , int d){
return abs(a - c) + abs(b - d);
}
void Goldik(){
cin >> n;
int ans = 0;
for(int i = 1 ; i <= n * 2 ; i++){
cin >> a[i] >> b[i];
if(a[i] <= n && b[i] <= 2 && a[i] >= 1 && b[i] >= 1){
p[a[i]][b[i]]++;
continue;
}
if(a[i] >= 1 && a[i] <= n){
if(b[i] < 1){
p[a[i]][1]++;
ans += abs(1 - b[i]);
continue;
}
p[a[i]][2]++;
ans += abs(b[i] - 2);
continue;
}
if(b[i] >= 1 && b[i] <= 2){
if(a[i] < 1){
p[1][b[i]]++;
ans += abs(1 - a[i]);
}
else{
p[n][b[i]]++;
ans += abs(a[i] - n);
}
continue;
}
int x = dist(a[i] , b[i] , 1 , 1) , y = dist(a[i] , b[i] , n , 1) , z = dist(a[i] , b[i] , 1 , 2) , w = dist(a[i] , b[i] , n , 2);
int mn = min({x , y , z , w});
if(mn == x){
ans += x;
p[1][1]++;
continue;
}
if(mn == y){
ans += y;
p[n][1]++;
continue;
}
if(mn == z){
ans += z;
p[1][2]++;
continue;
}
if(mn == w){
ans += w;
p[n][2]++;
continue;
}
}
// for(int i = 1 ; i <= 2 ; i++){
// for(int j = 1 ; j <= n ; j++){
// cout << p[j][i] << ' ';
// }
// cout << '\n';
// }
for(int i = 1 ; i <= n ; i++){
// cout << "i = " << i << '\n';
if(p[i][1] <= 0 && p[i][2] <= 0){
p[i + 1][1] += (p[i][1] - 1);
p[i + 1][2] += (p[i][2] - 1);
ans += abs(p[i][1] - 1) + abs(p[i][2] - 1);
// cout << abs(p[i][1] - 1) + abs(p[i][2] - 1) << '\n';
p[i][1] = 1;
p[i][2] = 1;
continue;
}
if(p[i][1] > 0 && p[i][2] > 0){
p[i + 1][1] += (p[i][1] - 1);
p[i + 1][2] += (p[i][2] - 1);
ans += abs(p[i][1] - 1) + abs(p[i][2] - 1);
// cout << abs(p[i][1] - 1) + abs(p[i][2] - 1) << '\n';
p[i][1] = 1;
p[i][2] = 1;
continue;
}
if(p[i][1] <= 0 && p[i][2] > 0){
int y = 1 - p[i][1];
if(p[i][2] - 1 >= y){
p[i][2] -= y;
p[i][1] += y;
ans += y;
// cout << y << '\n';
}
else{
int z = p[i][2] - 1;
p[i][2] -= z;
p[i][1] += z;
ans += z;
// cout << z << '\n';
}
p[i + 1][1] += (p[i][1] - 1);
p[i + 1][2] += (p[i][2] - 1);
ans += abs(p[i][1] - 1) + abs(p[i][2] - 1);
// cout << abs(p[i][1] - 1) + abs(p[i][2] - 1) << '\n';
p[i][1] = 1;
p[i][2] = 1;
continue;
}
// cout << "P = " << p[i][1] << ' ' << p[i][2] << '\n';
int y = 1 - p[i][2];
// cout << "y = " << y << '\n';
if(p[i][1] - 1 >= y){
p[i][1] -= y;
p[i][2] += y;
ans += y;
// cout << y << '\n';
}
else{
int z = p[i][1] - 1;
// cout << "z = " << z << '\n';
p[i][1] -= z;
p[i][2] += z;
ans += z;
// cout << z << '\n';
}
p[i + 1][1] += (p[i][1] - 1);
p[i + 1][2] += (p[i][2] - 1);
ans += abs(p[i][1] - 1) + abs(p[i][2] - 1);
// cout << abs(p[i][1] - 1) + abs(p[i][2] - 1) << '\n';
p[i][1] = 1;
p[i][2] = 1;
continue;
}
cout << ans << '\n';
}
//rewai mnogo zadach vozmozhno odna iz nih gde to popadetsya
//returning winter prime?
//chem prowe tem luchshe
signed main(/*AZ AZDAN UZDIKSIZ*/){
//freopen("txt.in","r",stdin);
//freopen("txt.out","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
srand(time(0));
int TT = 1;
// cin >> TT;
for(int i = 1 ; i <= TT ; i++){
//cout << "Case " << i << ": ";
Goldik();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
1 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
1 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
4432 KB |
Output is correct |
9 |
Correct |
1 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
4432 KB |
Output is correct |
11 |
Correct |
1 ms |
4432 KB |
Output is correct |
12 |
Correct |
1 ms |
4432 KB |
Output is correct |
13 |
Correct |
1 ms |
4432 KB |
Output is correct |
14 |
Correct |
1 ms |
4432 KB |
Output is correct |
15 |
Correct |
1 ms |
4564 KB |
Output is correct |
16 |
Correct |
1 ms |
4432 KB |
Output is correct |
17 |
Correct |
1 ms |
4600 KB |
Output is correct |
18 |
Correct |
1 ms |
4432 KB |
Output is correct |
19 |
Correct |
1 ms |
4432 KB |
Output is correct |
20 |
Correct |
1 ms |
4600 KB |
Output is correct |
21 |
Correct |
1 ms |
4432 KB |
Output is correct |
22 |
Correct |
1 ms |
4432 KB |
Output is correct |
23 |
Correct |
1 ms |
4432 KB |
Output is correct |
24 |
Correct |
1 ms |
4684 KB |
Output is correct |
25 |
Correct |
1 ms |
4432 KB |
Output is correct |
26 |
Correct |
1 ms |
4432 KB |
Output is correct |
27 |
Correct |
1 ms |
4432 KB |
Output is correct |
28 |
Correct |
1 ms |
4560 KB |
Output is correct |
29 |
Correct |
2 ms |
4444 KB |
Output is correct |
30 |
Correct |
1 ms |
4432 KB |
Output is correct |
31 |
Correct |
1 ms |
4572 KB |
Output is correct |
32 |
Correct |
1 ms |
4432 KB |
Output is correct |
33 |
Correct |
1 ms |
4432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
1 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
1 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
4432 KB |
Output is correct |
9 |
Correct |
1 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
4432 KB |
Output is correct |
11 |
Correct |
1 ms |
4432 KB |
Output is correct |
12 |
Correct |
1 ms |
4432 KB |
Output is correct |
13 |
Correct |
1 ms |
4432 KB |
Output is correct |
14 |
Correct |
1 ms |
4432 KB |
Output is correct |
15 |
Correct |
1 ms |
4564 KB |
Output is correct |
16 |
Correct |
1 ms |
4432 KB |
Output is correct |
17 |
Correct |
1 ms |
4600 KB |
Output is correct |
18 |
Correct |
1 ms |
4432 KB |
Output is correct |
19 |
Correct |
1 ms |
4432 KB |
Output is correct |
20 |
Correct |
1 ms |
4600 KB |
Output is correct |
21 |
Correct |
1 ms |
4432 KB |
Output is correct |
22 |
Correct |
1 ms |
4432 KB |
Output is correct |
23 |
Correct |
1 ms |
4432 KB |
Output is correct |
24 |
Correct |
1 ms |
4684 KB |
Output is correct |
25 |
Correct |
1 ms |
4432 KB |
Output is correct |
26 |
Correct |
1 ms |
4432 KB |
Output is correct |
27 |
Correct |
1 ms |
4432 KB |
Output is correct |
28 |
Correct |
1 ms |
4560 KB |
Output is correct |
29 |
Correct |
2 ms |
4444 KB |
Output is correct |
30 |
Correct |
1 ms |
4432 KB |
Output is correct |
31 |
Correct |
1 ms |
4572 KB |
Output is correct |
32 |
Correct |
1 ms |
4432 KB |
Output is correct |
33 |
Correct |
1 ms |
4432 KB |
Output is correct |
34 |
Correct |
1 ms |
4600 KB |
Output is correct |
35 |
Correct |
1 ms |
4432 KB |
Output is correct |
36 |
Correct |
1 ms |
4432 KB |
Output is correct |
37 |
Correct |
1 ms |
4432 KB |
Output is correct |
38 |
Correct |
1 ms |
4432 KB |
Output is correct |
39 |
Correct |
1 ms |
4432 KB |
Output is correct |
40 |
Correct |
1 ms |
4432 KB |
Output is correct |
41 |
Correct |
1 ms |
4432 KB |
Output is correct |
42 |
Correct |
1 ms |
4688 KB |
Output is correct |
43 |
Correct |
1 ms |
4436 KB |
Output is correct |
44 |
Correct |
1 ms |
4440 KB |
Output is correct |
45 |
Correct |
2 ms |
4776 KB |
Output is correct |
46 |
Correct |
2 ms |
4440 KB |
Output is correct |
47 |
Correct |
2 ms |
4440 KB |
Output is correct |
48 |
Correct |
2 ms |
4432 KB |
Output is correct |
49 |
Correct |
1 ms |
4432 KB |
Output is correct |
50 |
Correct |
1 ms |
4432 KB |
Output is correct |
51 |
Correct |
1 ms |
4620 KB |
Output is correct |
52 |
Correct |
1 ms |
4432 KB |
Output is correct |
53 |
Correct |
1 ms |
4432 KB |
Output is correct |
54 |
Correct |
1 ms |
4432 KB |
Output is correct |
55 |
Correct |
2 ms |
4432 KB |
Output is correct |
56 |
Correct |
1 ms |
4432 KB |
Output is correct |
57 |
Correct |
1 ms |
4432 KB |
Output is correct |
58 |
Correct |
1 ms |
4432 KB |
Output is correct |
59 |
Correct |
1 ms |
4432 KB |
Output is correct |
60 |
Correct |
1 ms |
4432 KB |
Output is correct |
61 |
Correct |
2 ms |
4432 KB |
Output is correct |
62 |
Correct |
1 ms |
4628 KB |
Output is correct |
63 |
Correct |
1 ms |
4588 KB |
Output is correct |
64 |
Correct |
1 ms |
4792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
1 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
1 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
4432 KB |
Output is correct |
9 |
Correct |
1 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
4432 KB |
Output is correct |
11 |
Correct |
1 ms |
4432 KB |
Output is correct |
12 |
Correct |
1 ms |
4432 KB |
Output is correct |
13 |
Correct |
1 ms |
4432 KB |
Output is correct |
14 |
Correct |
1 ms |
4432 KB |
Output is correct |
15 |
Correct |
1 ms |
4564 KB |
Output is correct |
16 |
Correct |
1 ms |
4432 KB |
Output is correct |
17 |
Correct |
1 ms |
4600 KB |
Output is correct |
18 |
Correct |
1 ms |
4432 KB |
Output is correct |
19 |
Correct |
1 ms |
4432 KB |
Output is correct |
20 |
Correct |
1 ms |
4600 KB |
Output is correct |
21 |
Correct |
1 ms |
4432 KB |
Output is correct |
22 |
Correct |
1 ms |
4432 KB |
Output is correct |
23 |
Correct |
1 ms |
4432 KB |
Output is correct |
24 |
Correct |
1 ms |
4684 KB |
Output is correct |
25 |
Correct |
1 ms |
4432 KB |
Output is correct |
26 |
Correct |
1 ms |
4432 KB |
Output is correct |
27 |
Correct |
1 ms |
4432 KB |
Output is correct |
28 |
Correct |
1 ms |
4560 KB |
Output is correct |
29 |
Correct |
2 ms |
4444 KB |
Output is correct |
30 |
Correct |
1 ms |
4432 KB |
Output is correct |
31 |
Correct |
1 ms |
4572 KB |
Output is correct |
32 |
Correct |
1 ms |
4432 KB |
Output is correct |
33 |
Correct |
1 ms |
4432 KB |
Output is correct |
34 |
Correct |
1 ms |
4600 KB |
Output is correct |
35 |
Correct |
1 ms |
4432 KB |
Output is correct |
36 |
Correct |
1 ms |
4432 KB |
Output is correct |
37 |
Correct |
1 ms |
4432 KB |
Output is correct |
38 |
Correct |
1 ms |
4432 KB |
Output is correct |
39 |
Correct |
1 ms |
4432 KB |
Output is correct |
40 |
Correct |
1 ms |
4432 KB |
Output is correct |
41 |
Correct |
1 ms |
4432 KB |
Output is correct |
42 |
Correct |
1 ms |
4688 KB |
Output is correct |
43 |
Correct |
1 ms |
4436 KB |
Output is correct |
44 |
Correct |
1 ms |
4440 KB |
Output is correct |
45 |
Correct |
2 ms |
4776 KB |
Output is correct |
46 |
Correct |
2 ms |
4440 KB |
Output is correct |
47 |
Correct |
2 ms |
4440 KB |
Output is correct |
48 |
Correct |
2 ms |
4432 KB |
Output is correct |
49 |
Correct |
1 ms |
4432 KB |
Output is correct |
50 |
Correct |
1 ms |
4432 KB |
Output is correct |
51 |
Correct |
1 ms |
4620 KB |
Output is correct |
52 |
Correct |
1 ms |
4432 KB |
Output is correct |
53 |
Correct |
1 ms |
4432 KB |
Output is correct |
54 |
Correct |
1 ms |
4432 KB |
Output is correct |
55 |
Correct |
2 ms |
4432 KB |
Output is correct |
56 |
Correct |
1 ms |
4432 KB |
Output is correct |
57 |
Correct |
1 ms |
4432 KB |
Output is correct |
58 |
Correct |
1 ms |
4432 KB |
Output is correct |
59 |
Correct |
1 ms |
4432 KB |
Output is correct |
60 |
Correct |
1 ms |
4432 KB |
Output is correct |
61 |
Correct |
2 ms |
4432 KB |
Output is correct |
62 |
Correct |
1 ms |
4628 KB |
Output is correct |
63 |
Correct |
1 ms |
4588 KB |
Output is correct |
64 |
Correct |
1 ms |
4792 KB |
Output is correct |
65 |
Correct |
19 ms |
10840 KB |
Output is correct |
66 |
Correct |
36 ms |
13924 KB |
Output is correct |
67 |
Correct |
28 ms |
12376 KB |
Output is correct |
68 |
Correct |
37 ms |
12556 KB |
Output is correct |
69 |
Correct |
35 ms |
12376 KB |
Output is correct |
70 |
Correct |
28 ms |
12384 KB |
Output is correct |
71 |
Correct |
28 ms |
12376 KB |
Output is correct |
72 |
Correct |
29 ms |
12380 KB |
Output is correct |
73 |
Correct |
29 ms |
12384 KB |
Output is correct |
74 |
Correct |
27 ms |
12384 KB |
Output is correct |
75 |
Correct |
27 ms |
12364 KB |
Output is correct |
76 |
Correct |
27 ms |
12364 KB |
Output is correct |
77 |
Correct |
29 ms |
12376 KB |
Output is correct |
78 |
Correct |
27 ms |
12372 KB |
Output is correct |
79 |
Correct |
27 ms |
12372 KB |
Output is correct |
80 |
Correct |
27 ms |
12364 KB |
Output is correct |
81 |
Correct |
27 ms |
12576 KB |
Output is correct |
82 |
Correct |
27 ms |
12384 KB |
Output is correct |
83 |
Correct |
27 ms |
12372 KB |
Output is correct |
84 |
Correct |
27 ms |
12520 KB |
Output is correct |
85 |
Correct |
27 ms |
12372 KB |
Output is correct |
86 |
Correct |
27 ms |
12372 KB |
Output is correct |
87 |
Correct |
30 ms |
12492 KB |
Output is correct |
88 |
Correct |
30 ms |
12372 KB |
Output is correct |
89 |
Correct |
28 ms |
12372 KB |
Output is correct |
90 |
Correct |
28 ms |
12372 KB |
Output is correct |
91 |
Correct |
28 ms |
12540 KB |
Output is correct |
92 |
Correct |
28 ms |
12620 KB |
Output is correct |
93 |
Correct |
28 ms |
12596 KB |
Output is correct |
94 |
Correct |
29 ms |
12392 KB |
Output is correct |
95 |
Correct |
26 ms |
12296 KB |
Output is correct |
96 |
Correct |
27 ms |
12532 KB |
Output is correct |
97 |
Correct |
27 ms |
12364 KB |
Output is correct |
98 |
Correct |
27 ms |
12372 KB |
Output is correct |
99 |
Correct |
28 ms |
12384 KB |
Output is correct |
100 |
Correct |
32 ms |
12376 KB |
Output is correct |
101 |
Correct |
30 ms |
12372 KB |
Output is correct |
102 |
Correct |
31 ms |
12364 KB |
Output is correct |
103 |
Correct |
30 ms |
12368 KB |
Output is correct |