#include "bits/stdc++.h"
using namespace std;
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
using ll = long long;
using ld = long double;
#define pb push_back
#define ff first
#define ss second
#define sz(x) (ll)(x).size()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
void freopen(string s) { freopen((s+".in").c_str(), "r", stdin); freopen((s+".out").c_str(), "w", stdout); }
void IOIGold2024_InshAllah() { ios_base::sync_with_stdio(false); cin.tie(NULL); }
ll binmul(ll a, ll b, ll c) { ll res = 0; while(b) { if(b&1) (res += a) %= c; (a += a) %= c; b >>= 1; } return res; }
ll binpow(ll a, ll b, ll c) { ll res = 1; while(b) { if(b&1) (res *= a) %= c; (a *= a) %= c; b >>= 1; } return res; }
template<typename T> T gcd(T a, T b) { if(b==0) return a; return gcd(b, a%b); }
template<typename T> T lcm(T a, T b) { return a/gcd(a, b)*b; }
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ld rnd() { return rng()%INT_MAX*1.0/INT_MAX; }
const ll inf = 1e18+7, MX = LLONG_MAX, MN = LLONG_MIN;
const ll mod = 1e9+7, N = 3e5+5;
ll cnt[N][3];
void kigash() {
ll n, ans = 0;
cin>>n;
for(ll i=1; i<=2*n; i++) {
ll x, y, X = -1, Y = -1;
cin>>x>>y;
if(x>=1 && x<=n && y>=2) X = x, Y = 2;
else if(x>=1 && x<=n && y<=1) X = x, Y = 1;
else if(y>=1 && y<=2 && x>=n) X = n, Y = y;
else if(y>=1 && y<=2 && x<=1) X = 1, Y = y;
else if(x<1 && y<1) X = 1, Y = 1;
else if(x<1 && y>2) X = 1, Y = 2;
else if(x>n && y<1) X = n, Y = 1;
else X = n, Y = 2;
cnt[X][Y]++;
ans += abs(X-x)+abs(Y-y);
}
ll sum1 = 0, sum2 = 0;
for(ll i=1; i<=n; i++) {
sum1 += cnt[i][1]-1, sum2 += cnt[i][2]-1;
if(sum1<0 && sum2>0) {
ll mn = min(abs(sum1), abs(sum2));
sum1 += mn, sum2 -= mn, ans += mn;
}
if(sum2<0 && sum1>0) {
ll mn = min(abs(sum1), abs(sum2));
sum1 -= mn, sum2 += mn, ans += mn;
}
ans += abs(sum1)+abs(sum2);
}
cout<<ans<<"\n";
return;
}
signed main(/*Kigash Amir*/) {
// freopen("");
IOIGold2024_InshAllah();
ll tt = 1;
// cin>>tt;
for(ll i=1; i<=tt; i++) {
kigash();
}
}
Compilation message
joi2019_ho_t4.cpp: In function 'void freopen(std::string)':
joi2019_ho_t4.cpp:17:33: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | void freopen(string s) { freopen((s+".in").c_str(), "r", stdin); freopen((s+".out").c_str(), "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t4.cpp:17:73: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | void freopen(string s) { freopen((s+".in").c_str(), "r", stdin); freopen((s+".out").c_str(), "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
472 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
472 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
456 KB |
Output is correct |
27 |
Correct |
0 ms |
468 KB |
Output is correct |
28 |
Correct |
0 ms |
344 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
472 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
472 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
456 KB |
Output is correct |
27 |
Correct |
0 ms |
468 KB |
Output is correct |
28 |
Correct |
0 ms |
344 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
464 KB |
Output is correct |
34 |
Correct |
1 ms |
356 KB |
Output is correct |
35 |
Correct |
1 ms |
352 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
352 KB |
Output is correct |
38 |
Correct |
1 ms |
352 KB |
Output is correct |
39 |
Correct |
1 ms |
352 KB |
Output is correct |
40 |
Correct |
1 ms |
352 KB |
Output is correct |
41 |
Correct |
1 ms |
352 KB |
Output is correct |
42 |
Correct |
1 ms |
352 KB |
Output is correct |
43 |
Correct |
1 ms |
356 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
1 ms |
348 KB |
Output is correct |
50 |
Correct |
1 ms |
348 KB |
Output is correct |
51 |
Correct |
1 ms |
348 KB |
Output is correct |
52 |
Correct |
1 ms |
348 KB |
Output is correct |
53 |
Correct |
1 ms |
348 KB |
Output is correct |
54 |
Correct |
1 ms |
348 KB |
Output is correct |
55 |
Correct |
1 ms |
344 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
348 KB |
Output is correct |
58 |
Correct |
1 ms |
348 KB |
Output is correct |
59 |
Correct |
1 ms |
344 KB |
Output is correct |
60 |
Correct |
1 ms |
348 KB |
Output is correct |
61 |
Correct |
1 ms |
348 KB |
Output is correct |
62 |
Correct |
1 ms |
348 KB |
Output is correct |
63 |
Correct |
1 ms |
344 KB |
Output is correct |
64 |
Correct |
1 ms |
520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
472 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
472 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
456 KB |
Output is correct |
27 |
Correct |
0 ms |
468 KB |
Output is correct |
28 |
Correct |
0 ms |
344 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
464 KB |
Output is correct |
34 |
Correct |
1 ms |
356 KB |
Output is correct |
35 |
Correct |
1 ms |
352 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
352 KB |
Output is correct |
38 |
Correct |
1 ms |
352 KB |
Output is correct |
39 |
Correct |
1 ms |
352 KB |
Output is correct |
40 |
Correct |
1 ms |
352 KB |
Output is correct |
41 |
Correct |
1 ms |
352 KB |
Output is correct |
42 |
Correct |
1 ms |
352 KB |
Output is correct |
43 |
Correct |
1 ms |
356 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
1 ms |
348 KB |
Output is correct |
50 |
Correct |
1 ms |
348 KB |
Output is correct |
51 |
Correct |
1 ms |
348 KB |
Output is correct |
52 |
Correct |
1 ms |
348 KB |
Output is correct |
53 |
Correct |
1 ms |
348 KB |
Output is correct |
54 |
Correct |
1 ms |
348 KB |
Output is correct |
55 |
Correct |
1 ms |
344 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
348 KB |
Output is correct |
58 |
Correct |
1 ms |
348 KB |
Output is correct |
59 |
Correct |
1 ms |
344 KB |
Output is correct |
60 |
Correct |
1 ms |
348 KB |
Output is correct |
61 |
Correct |
1 ms |
348 KB |
Output is correct |
62 |
Correct |
1 ms |
348 KB |
Output is correct |
63 |
Correct |
1 ms |
344 KB |
Output is correct |
64 |
Correct |
1 ms |
520 KB |
Output is correct |
65 |
Correct |
26 ms |
6228 KB |
Output is correct |
66 |
Correct |
36 ms |
5052 KB |
Output is correct |
67 |
Correct |
31 ms |
7760 KB |
Output is correct |
68 |
Correct |
44 ms |
7800 KB |
Output is correct |
69 |
Correct |
35 ms |
7672 KB |
Output is correct |
70 |
Correct |
33 ms |
7776 KB |
Output is correct |
71 |
Correct |
55 ms |
7768 KB |
Output is correct |
72 |
Correct |
28 ms |
5708 KB |
Output is correct |
73 |
Correct |
29 ms |
5460 KB |
Output is correct |
74 |
Correct |
32 ms |
5860 KB |
Output is correct |
75 |
Correct |
30 ms |
5560 KB |
Output is correct |
76 |
Correct |
42 ms |
5712 KB |
Output is correct |
77 |
Correct |
29 ms |
5468 KB |
Output is correct |
78 |
Correct |
28 ms |
5564 KB |
Output is correct |
79 |
Correct |
29 ms |
5568 KB |
Output is correct |
80 |
Correct |
29 ms |
5468 KB |
Output is correct |
81 |
Correct |
35 ms |
5696 KB |
Output is correct |
82 |
Correct |
29 ms |
5704 KB |
Output is correct |
83 |
Correct |
29 ms |
7516 KB |
Output is correct |
84 |
Correct |
31 ms |
7644 KB |
Output is correct |
85 |
Correct |
32 ms |
7636 KB |
Output is correct |
86 |
Correct |
29 ms |
7764 KB |
Output is correct |
87 |
Correct |
36 ms |
7760 KB |
Output is correct |
88 |
Correct |
30 ms |
7904 KB |
Output is correct |
89 |
Correct |
38 ms |
7792 KB |
Output is correct |
90 |
Correct |
29 ms |
7764 KB |
Output is correct |
91 |
Correct |
29 ms |
7772 KB |
Output is correct |
92 |
Correct |
30 ms |
7936 KB |
Output is correct |
93 |
Correct |
31 ms |
7804 KB |
Output is correct |
94 |
Correct |
41 ms |
7780 KB |
Output is correct |
95 |
Correct |
28 ms |
7764 KB |
Output is correct |
96 |
Correct |
30 ms |
7760 KB |
Output is correct |
97 |
Correct |
30 ms |
7760 KB |
Output is correct |
98 |
Correct |
29 ms |
5724 KB |
Output is correct |
99 |
Correct |
47 ms |
5692 KB |
Output is correct |
100 |
Correct |
34 ms |
5696 KB |
Output is correct |
101 |
Correct |
32 ms |
5560 KB |
Output is correct |
102 |
Correct |
41 ms |
5696 KB |
Output is correct |
103 |
Correct |
33 ms |
5716 KB |
Output is correct |