#include "bits/stdc++.h"
using namespace std;
#define int long long
#define double long double
const int MAXN = 2e5 + 10;
const int MOD = 1e9 + 7;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
int bm(int b, int p) {
if(p==0) return 1 % MOD;
int r = bm(b, p >> 1);
if(p&1) return (((r*r) % MOD) * b) % MOD;
return (r*r) % MOD;
}
int inv(int b) {
return bm(b, MOD-2);
}
int fastlog(int x) {
return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1);
}
void printcase(int i) { cout << "Case #" << i << ": "; }
static void run_with_stack_size(void (*func)(void), size_t stsize) {
char *stack, *send;
stack = (char *)malloc(stsize);
send = stack + stsize - 16;
send = (char *)((uintptr_t)send / 16 * 16);
asm volatile(
"mov %%rsp, (%0)\n"
"mov %0, %%rsp\n"
:
: "r"(send));
func();
asm volatile("mov (%0), %%rsp\n" : : "r"(send));
free(stack);
}
void solve(int tc) {
int n;
cin >> n;
int ans = 0;
int x[2*n+1], y[2*n+1];
for(int i=1; i<=2*n; i++) {
cin >> x[i] >> y[i];
if(x[i] < 1) {
ans += abs(x[i]) + 1;
x[i] = 1;
}
if(x[i] > n) {
ans += x[i] - n;
x[i] = n;
}
if(y[i] < 1) {
ans += abs(y[i]) + 1;
y[i] = 1;
}
if(y[i] > 2) {
ans += y[i] - 2;
y[i] = 2;
}
}
int coins[n+1][3];
for(int i=1; i<=n; i++) {
for(int j=1; j<=2; j++) coins[i][j] = 0;
}
for(int i=1; i<=2*n; i++) {
coins[x[i]][y[i]]++;
}
vector<pair<int, int>> l, r;
for(int i=1; i<=n; i++) {
for(int j=1; j<=2; j++) {
if(!coins[i][j]) r.push_back({i, j});
for(int k=1;k<coins[i][j]; k++) l.push_back({i, j});
}
}
int bruh = 1e18;
sort(r.begin(), r.end());
do {
int totn = 0;
for(int i=0; i<l.size(); i++) {
totn += abs(l[i].first - r[i].first);
totn += abs(l[i].second - r[i].second);
}
bruh = min(bruh, totn);
} while(next_permutation(r.begin(), r.end()));
ans += bruh;
cout << ans << '\n';
}
void uwu() {
ios::sync_with_stdio(0); cin.tie(0);
int t = 1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
int32_t main() {
#ifdef ONLINE_JUDGE
uwu();
#endif
#ifndef ONLINE_JUDGE
run_with_stack_size(uwu, 1024 * 1024 * 1024); // run with a 1 GiB stack
#endif
}
/*
g++ brute.cpp -std=c++17 -O2 -o brute
./brute < input.txt
*/
Compilation message
joi2019_ho_t4.cpp: In function 'void solve(long long int)':
joi2019_ho_t4.cpp:80:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | for(int i=0; i<l.size(); i++) {
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
5 |
Execution timed out |
1092 ms |
348 KB |
Time limit exceeded |
6 |
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 |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Execution timed out |
1092 ms |
348 KB |
Time limit exceeded |
6 |
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 |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Execution timed out |
1092 ms |
348 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |