//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,sse,sse2,sse3,ssse3,sse4,abm,popcnt,mmx")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef complex<double> cd;
constexpr ll INF64 = 9'000'000'000'000'000'000, INF32 = 2'000'000'000, MOD = 1'000'000'007;
constexpr db PI = acos(-1);
constexpr bool IS_FILE = false, IS_TEST_CASES = false;
random_device rd;
mt19937 rnd32(rd());
mt19937_64 rnd64(rd());
template<typename T>
bool assign_max(T& a, T b) {
if (b > a) {
a = b;
return true;
}
return false;
}
template<typename T>
bool assign_min(T& a, T b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<typename T>
T square(T a) {
return a * a;
}
template<>
struct std::hash<pair<ll, ll>> {
ll operator() (pair<ll, ll> p) const {
return ((__int128)p.first * MOD + p.second) % INF64;
}
};
ll get(ll n, deque<ll> all1, deque<ll> all2) {
ll ans = 0;
ll nc1 = 0, nc2 = 0;
for (ll i = 0; i < n; i++) {
ans += nc1 + nc2;
while (!all1.empty() && all1.front() == i) {
nc1++;
all1.pop_front();
}
while (!all2.empty() && all2.front() == i) {
nc2++;
all2.pop_front();
}
bool b1 = false, b2 = false;
if (nc1) {
nc1--;
b1 = true;
}
if (nc2) {
nc2--;
b2 = true;
}
if (!b1 && !b2) {
ll ba = INF64;
ll d1 = 0, d2 = 0;
if (!all1.empty() && !all2.empty()) {
if (assign_min(ba, all1[0] + all2[0] - i * 2)) {
d1 = 1;
d2 = 1;
}
} else {
if (all1.size() >= 2) {
if (assign_min(ba, all1[0] + all1[1] - i * 2 + 1)) {
d1 = 2;
d2 = 0;
}
}
if (all2.size() >= 2) {
if (assign_min(ba, all2[0] + all2[1] - i * 2 + 1)) {
d1 = 0;
d2 = 2;
}
}
}
ans += ba;
for (ll i = 0; i < d1; i++) {
all1.pop_front();
}
for (ll i = 0; i < d2; i++) {
all2.pop_front();
}
b1 = true;
b2 = true;
}
if (!b1) {
if (nc2) {
nc2--;
ans++;
continue;
}
ll ba = INF64;
bool u1;
if (!all1.empty()) {
if (assign_min(ba, all1[0] - i)) {
u1 = true;
}
} else {
if (assign_min(ba, all2[0] - i + 1)) {
u1 = false;
}
}
ans += ba;
if (u1) {
all1.pop_front();
} else {
all2.pop_front();
}
}
if (!b2) {
if (nc1) {
nc1--;
ans++;
continue;
}
ll ba = INF64;
bool u1;
if (!all2.empty()) {
if (assign_min(ba, all2[0] - i)) {
u1 = false;
}
} else {
if (assign_min(ba, all1[0] - i + 1)) {
u1 = true;
}
}
ans += ba;
if (u1) {
all1.pop_front();
} else {
all2.pop_front();
}
}
}
return ans;
}
void solve() {
ll n;
cin >> n;
vector<ll> col1(n, 0), col2(n, 0);
ll ans = 0;
for (ll i = 0; i < n * 2; i++) {
ll x, y;
cin >> x >> y;
if (x < 1) {
ans += 1 - x;
x = 1;
}
if (x > n) {
ans += x - n;
x = n;
}
x--;
if (y >= 2) {
col2[x]++;
ans += y - 2;
} else {
col1[x]++;
ans += 1 - y;
}
}
deque<ll> all11, all21, all22, all12;
for (ll i = 0; i < n; i++) {
for (ll j = 0; j < col1[i]; j++) {
all11.push_back(i);
}
for (ll j = 0; j < col2[i]; j++) {
all21.push_back(i);
}
for (ll j = 0; j < col1[n - i - 1]; j++) {
all12.push_back(i);
}
for (ll j = 0; j < col2[n - i - 1]; j++) {
all22.push_back(i);
}
}
cout << ans + min(get(n, all11, all21), get(n, all12, all22)) << '\n';
}
int main() {
if (IS_FILE) {
freopen("", "r", stdin);
freopen("", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll t = 1;
if (IS_TEST_CASES) {
cin >> t;
}
for (ll i = 0; i < t; i++) {
solve();
}
}
Compilation message
joi2019_ho_t4.cpp: In function 'int main()':
joi2019_ho_t4.cpp:201:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
201 | freopen("", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~
joi2019_ho_t4.cpp:202:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
202 | freopen("", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~
joi2019_ho_t4.cpp: In function 'll get(ll, std::deque<long long int>, std::deque<long long int>)':
joi2019_ho_t4.cpp:146:25: warning: 'u1' may be used uninitialized in this function [-Wmaybe-uninitialized]
146 | if (u1) {
| ^~
joi2019_ho_t4.cpp:122:25: warning: 'u1' may be used uninitialized in this function [-Wmaybe-uninitialized]
122 | if (u1) {
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
600 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
344 KB |
Output is correct |
24 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
25 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
600 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
344 KB |
Output is correct |
24 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
25 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
600 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
344 KB |
Output is correct |
24 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
25 |
Halted |
0 ms |
0 KB |
- |