#include "bits/stdc++.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define ph push
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ((ll)x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define btinpct(x) __builtin_popcountll((x))
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) { return (rng() % (y+1-x)) + x; } //inclusivesss
string to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){if(a>b)swap(a,b);if(a==0)return b;return gcd(b%a,a);}
#define ll long long int
#define ld long double
#define FOR(ii, ss, ee) for(ll ii = (ss); ii <= (ll)(ee); ++ii)
#define DEC(ii, ss, ee) for(ll ii = (ss); ii >= (ll)(ee); --ii)
typedef pair <ll, ll> pi; typedef pair <ll, pi> spi; typedef pair <pi, pi> dpi;
#define LLINF ((long long) 1e18)//1234567890987654321
#define INF 1234567890ll
// #define cerr if(0)cout
#define MAXN (200006)
ll n, ans;
pi A[MAXN];
int col[MAXN/2][3];
void add(ll a,ll b,ll c,ll d) {
ans += llabs(a-c) + llabs(b-d);
}
int main()
{
FAST
cin>>n;
FOR(i,1,2*n) cin>>A[i].f>>A[i].s;
FOR(i,1,2*n) {
if(1<=A[i].f&&A[i].f<=n&&1<=A[i].s&&A[i].s<=2) ++ col[A[i].f][A[i].s];
else if(1 <= A[i].f && A[i].f <= n) ++ col[A[i].f][(A[i].s>2?2:1)], add(A[i].f, A[i].s, A[i].f, (A[i].s>2?2:1));
else if(A[i].f < 1 && A[i].s >= 2) ++ col[1][2], add(A[i].f, A[i].s, 1, 2);
else if(A[i].f < 1 && A[i].s <= 1) ++ col[1][1], add(A[i].f, A[i].s, 1, 1);
else if(A[i].f > n && A[i].s >= 2) ++ col[n][2], add(A[i].f, A[i].s, n, 2);
else if(A[i].f > n && A[i].s <= 1) ++ col[n][1], add(A[i].f, A[i].s, n, 1);
else assert(0);
}
// cerr<<"ans: "<<ans<<"\n";
// FOR(j,1,2) FOR(i,1,n) cout<<col[i][j]<<" \n"[i==n];
int d1=0, d2=0;
FOR(i,1,n) {
d1 += col[i][1]-1;
d2 += col[i][2]-1;
if(d1 < 0 && d2 > 0) {
int tmp=min(d2, -d1);
ans += tmp, d1 += tmp, d2 -= tmp;
} else if(d1 > 0 && d2 < 0) {
int tmp=min(d1, -d2);
ans += tmp, d2 += tmp, d1 -= tmp;
}
ans += abs(d1);
ans += abs(d2);
}
cout<<ans<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
3 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
3 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
388 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
376 KB |
Output is correct |
28 |
Correct |
3 ms |
376 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
30 |
Correct |
2 ms |
376 KB |
Output is correct |
31 |
Correct |
2 ms |
376 KB |
Output is correct |
32 |
Correct |
2 ms |
376 KB |
Output is correct |
33 |
Correct |
2 ms |
388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
3 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
3 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
388 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
376 KB |
Output is correct |
28 |
Correct |
3 ms |
376 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
30 |
Correct |
2 ms |
376 KB |
Output is correct |
31 |
Correct |
2 ms |
376 KB |
Output is correct |
32 |
Correct |
2 ms |
376 KB |
Output is correct |
33 |
Correct |
2 ms |
388 KB |
Output is correct |
34 |
Correct |
3 ms |
380 KB |
Output is correct |
35 |
Correct |
3 ms |
504 KB |
Output is correct |
36 |
Correct |
3 ms |
400 KB |
Output is correct |
37 |
Correct |
3 ms |
376 KB |
Output is correct |
38 |
Correct |
3 ms |
424 KB |
Output is correct |
39 |
Correct |
3 ms |
376 KB |
Output is correct |
40 |
Correct |
3 ms |
376 KB |
Output is correct |
41 |
Correct |
3 ms |
376 KB |
Output is correct |
42 |
Correct |
3 ms |
376 KB |
Output is correct |
43 |
Correct |
3 ms |
396 KB |
Output is correct |
44 |
Correct |
3 ms |
376 KB |
Output is correct |
45 |
Correct |
5 ms |
376 KB |
Output is correct |
46 |
Correct |
3 ms |
376 KB |
Output is correct |
47 |
Correct |
3 ms |
504 KB |
Output is correct |
48 |
Correct |
3 ms |
376 KB |
Output is correct |
49 |
Correct |
3 ms |
380 KB |
Output is correct |
50 |
Correct |
3 ms |
376 KB |
Output is correct |
51 |
Correct |
3 ms |
376 KB |
Output is correct |
52 |
Correct |
2 ms |
376 KB |
Output is correct |
53 |
Correct |
3 ms |
504 KB |
Output is correct |
54 |
Correct |
3 ms |
376 KB |
Output is correct |
55 |
Correct |
3 ms |
504 KB |
Output is correct |
56 |
Correct |
3 ms |
376 KB |
Output is correct |
57 |
Correct |
3 ms |
376 KB |
Output is correct |
58 |
Correct |
3 ms |
376 KB |
Output is correct |
59 |
Correct |
3 ms |
504 KB |
Output is correct |
60 |
Correct |
3 ms |
376 KB |
Output is correct |
61 |
Correct |
3 ms |
376 KB |
Output is correct |
62 |
Correct |
3 ms |
376 KB |
Output is correct |
63 |
Correct |
2 ms |
376 KB |
Output is correct |
64 |
Correct |
3 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
3 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
3 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
388 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
376 KB |
Output is correct |
28 |
Correct |
3 ms |
376 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
30 |
Correct |
2 ms |
376 KB |
Output is correct |
31 |
Correct |
2 ms |
376 KB |
Output is correct |
32 |
Correct |
2 ms |
376 KB |
Output is correct |
33 |
Correct |
2 ms |
388 KB |
Output is correct |
34 |
Correct |
3 ms |
380 KB |
Output is correct |
35 |
Correct |
3 ms |
504 KB |
Output is correct |
36 |
Correct |
3 ms |
400 KB |
Output is correct |
37 |
Correct |
3 ms |
376 KB |
Output is correct |
38 |
Correct |
3 ms |
424 KB |
Output is correct |
39 |
Correct |
3 ms |
376 KB |
Output is correct |
40 |
Correct |
3 ms |
376 KB |
Output is correct |
41 |
Correct |
3 ms |
376 KB |
Output is correct |
42 |
Correct |
3 ms |
376 KB |
Output is correct |
43 |
Correct |
3 ms |
396 KB |
Output is correct |
44 |
Correct |
3 ms |
376 KB |
Output is correct |
45 |
Correct |
5 ms |
376 KB |
Output is correct |
46 |
Correct |
3 ms |
376 KB |
Output is correct |
47 |
Correct |
3 ms |
504 KB |
Output is correct |
48 |
Correct |
3 ms |
376 KB |
Output is correct |
49 |
Correct |
3 ms |
380 KB |
Output is correct |
50 |
Correct |
3 ms |
376 KB |
Output is correct |
51 |
Correct |
3 ms |
376 KB |
Output is correct |
52 |
Correct |
2 ms |
376 KB |
Output is correct |
53 |
Correct |
3 ms |
504 KB |
Output is correct |
54 |
Correct |
3 ms |
376 KB |
Output is correct |
55 |
Correct |
3 ms |
504 KB |
Output is correct |
56 |
Correct |
3 ms |
376 KB |
Output is correct |
57 |
Correct |
3 ms |
376 KB |
Output is correct |
58 |
Correct |
3 ms |
376 KB |
Output is correct |
59 |
Correct |
3 ms |
504 KB |
Output is correct |
60 |
Correct |
3 ms |
376 KB |
Output is correct |
61 |
Correct |
3 ms |
376 KB |
Output is correct |
62 |
Correct |
3 ms |
376 KB |
Output is correct |
63 |
Correct |
2 ms |
376 KB |
Output is correct |
64 |
Correct |
3 ms |
504 KB |
Output is correct |
65 |
Correct |
40 ms |
6236 KB |
Output is correct |
66 |
Correct |
68 ms |
8112 KB |
Output is correct |
67 |
Correct |
59 ms |
7804 KB |
Output is correct |
68 |
Correct |
59 ms |
7772 KB |
Output is correct |
69 |
Correct |
59 ms |
7800 KB |
Output is correct |
70 |
Correct |
58 ms |
7800 KB |
Output is correct |
71 |
Correct |
60 ms |
7772 KB |
Output is correct |
72 |
Correct |
62 ms |
6604 KB |
Output is correct |
73 |
Correct |
57 ms |
6696 KB |
Output is correct |
74 |
Correct |
57 ms |
6684 KB |
Output is correct |
75 |
Correct |
59 ms |
6620 KB |
Output is correct |
76 |
Correct |
58 ms |
6704 KB |
Output is correct |
77 |
Correct |
58 ms |
6648 KB |
Output is correct |
78 |
Correct |
58 ms |
6672 KB |
Output is correct |
79 |
Correct |
58 ms |
6736 KB |
Output is correct |
80 |
Correct |
57 ms |
6704 KB |
Output is correct |
81 |
Correct |
59 ms |
6648 KB |
Output is correct |
82 |
Correct |
69 ms |
6648 KB |
Output is correct |
83 |
Correct |
58 ms |
6648 KB |
Output is correct |
84 |
Correct |
58 ms |
7008 KB |
Output is correct |
85 |
Correct |
58 ms |
7032 KB |
Output is correct |
86 |
Correct |
59 ms |
7800 KB |
Output is correct |
87 |
Correct |
60 ms |
7800 KB |
Output is correct |
88 |
Correct |
60 ms |
7800 KB |
Output is correct |
89 |
Correct |
59 ms |
7800 KB |
Output is correct |
90 |
Correct |
59 ms |
7800 KB |
Output is correct |
91 |
Correct |
59 ms |
7792 KB |
Output is correct |
92 |
Correct |
58 ms |
7892 KB |
Output is correct |
93 |
Correct |
59 ms |
7928 KB |
Output is correct |
94 |
Correct |
61 ms |
7672 KB |
Output is correct |
95 |
Correct |
59 ms |
7672 KB |
Output is correct |
96 |
Correct |
58 ms |
7492 KB |
Output is correct |
97 |
Correct |
57 ms |
7416 KB |
Output is correct |
98 |
Correct |
58 ms |
7388 KB |
Output is correct |
99 |
Correct |
58 ms |
7160 KB |
Output is correct |
100 |
Correct |
59 ms |
7336 KB |
Output is correct |
101 |
Correct |
57 ms |
7420 KB |
Output is correct |
102 |
Correct |
62 ms |
7416 KB |
Output is correct |
103 |
Correct |
58 ms |
7192 KB |
Output is correct |