#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);
}
int r[3][MAXN], l[3][MAXN], ud[3][MAXN];
void solve(int tc) {
int n;
cin >> n;
int cnt[3][n+1];
for(int i=1; i<=2; i++) {
for(int j=1; j<=n; j++) {
cnt[i][j] = 0;
}
}
int ans = 0;
for(int i=1; i<=2*n; i++) {
int x, y;
cin >> x >> y;
if(x < 1) ans += 1 - x;
if(x > n) ans += x - n;
if(y < 1) ans += 1 - y;
if(y > 2) ans += y - 2;
x = max(x, 1ll);
y = max(y, 1ll);
x = min(x, n);
y = min(y, 2ll);
cnt[y][x]++;
}
vector<pair<int, int> > extra;
for(int i=1; i<=2; i++) {
for(int j=1; j<=n; j++) {
for(int k=1; k<cnt[i][j]; k++) extra.push_back({i, j});
}
}
for(pair<int,int> x: extra) {
int st = (x.first - 1) * n + x.second;
vector<pair<int,int>> adj[2*n+1];
for(int i=1; i<=2; i++) {
for(int j=1; j<=n; j++) {
int node = (i-1) * n + j;
if(j>1 && l[i][j]>=r[i][j-1]) adj[node].push_back({node-1, 1});
else if(j>1) adj[node].push_back({node-1, -1});
if(j<n && r[i][j]>=l[i][j+1]) adj[node].push_back({node+1, 1});
else if(j<n) adj[node].push_back({node+1, -1});
if(ud[i][j]>=ud[3-i][j]) adj[node].push_back({(2-i)*n+j, 1});
else adj[node].push_back({(2-i)*n+j, -1});
}
}
bool vis[2*n+1];
int dist[2*n+1], prv[2*n+1];
for(int i=1; i<=2*n; i++) {
dist[i] = 1e9;
vis[i] = 0;
}
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
dist[st] = 0;
pq.push({0, st});
while(pq.size()) {
pair<int, int> t = pq.top();
pq.pop();
for(pair<int, int> x : adj[t.second]) {
if(dist[x.first] > dist[t.second] + x.second) {
dist[x.first] = dist[t.second] + x.second;
prv[x.first] = t.second;
pq.push({dist[x.first], x.first});
}
}
}
int en = -1, mn = 1e9;
for(int i=1; i<=2; i++) {
for(int j=1; j<=n; j++) {
if(!cnt[i][j]) {
int id = (i-1) * n + j;
if(dist[id] < mn) {
mn = dist[id];
en = id;
}
}
}
}
cnt[(st-1)/n+1][(st-1)%n+1]--;
cnt[(en-1)/n+1][(en-1)%n+1]++;
ans += dist[en];
while(en != st) {
int p = prv[en];
if(p + n == en) {
ud[1][p]++;
}
else if(p - n == en) {
ud[2][p-n]++;
}
else if(p + 1 == en) {
if(p <= n) r[1][p]++;
else r[2][p-n]++;
}
else {
if(p <= n) l[1][p]++;
else l[2][p-n]++;
}
en = p;
}
}
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++ A.cpp -std=c++17 -O2 -o A
./A < input.txt
*/
Compilation message
joi2019_ho_t4.cpp: In function 'void solve(long long int)':
joi2019_ho_t4.cpp:82:10: warning: variable 'vis' set but not used [-Wunused-but-set-variable]
82 | bool vis[2*n+1];
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
596 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 |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 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 |
1 ms |
592 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
596 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 |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 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 |
1 ms |
592 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
314 ms |
872 KB |
Output is correct |
36 |
Correct |
162 ms |
1072 KB |
Output is correct |
37 |
Correct |
167 ms |
888 KB |
Output is correct |
38 |
Correct |
140 ms |
848 KB |
Output is correct |
39 |
Correct |
138 ms |
844 KB |
Output is correct |
40 |
Correct |
166 ms |
812 KB |
Output is correct |
41 |
Correct |
326 ms |
856 KB |
Output is correct |
42 |
Correct |
336 ms |
884 KB |
Output is correct |
43 |
Correct |
351 ms |
860 KB |
Output is correct |
44 |
Correct |
351 ms |
872 KB |
Output is correct |
45 |
Correct |
361 ms |
860 KB |
Output is correct |
46 |
Correct |
389 ms |
1096 KB |
Output is correct |
47 |
Correct |
356 ms |
904 KB |
Output is correct |
48 |
Correct |
355 ms |
880 KB |
Output is correct |
49 |
Correct |
375 ms |
852 KB |
Output is correct |
50 |
Correct |
380 ms |
1100 KB |
Output is correct |
51 |
Correct |
320 ms |
848 KB |
Output is correct |
52 |
Correct |
271 ms |
2856 KB |
Output is correct |
53 |
Correct |
225 ms |
812 KB |
Output is correct |
54 |
Correct |
198 ms |
848 KB |
Output is correct |
55 |
Correct |
202 ms |
3068 KB |
Output is correct |
56 |
Correct |
240 ms |
2900 KB |
Output is correct |
57 |
Correct |
295 ms |
848 KB |
Output is correct |
58 |
Correct |
275 ms |
844 KB |
Output is correct |
59 |
Correct |
275 ms |
852 KB |
Output is correct |
60 |
Correct |
279 ms |
844 KB |
Output is correct |
61 |
Correct |
257 ms |
1060 KB |
Output is correct |
62 |
Correct |
284 ms |
1104 KB |
Output is correct |
63 |
Correct |
286 ms |
868 KB |
Output is correct |
64 |
Correct |
265 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
596 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 |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 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 |
1 ms |
592 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
314 ms |
872 KB |
Output is correct |
36 |
Correct |
162 ms |
1072 KB |
Output is correct |
37 |
Correct |
167 ms |
888 KB |
Output is correct |
38 |
Correct |
140 ms |
848 KB |
Output is correct |
39 |
Correct |
138 ms |
844 KB |
Output is correct |
40 |
Correct |
166 ms |
812 KB |
Output is correct |
41 |
Correct |
326 ms |
856 KB |
Output is correct |
42 |
Correct |
336 ms |
884 KB |
Output is correct |
43 |
Correct |
351 ms |
860 KB |
Output is correct |
44 |
Correct |
351 ms |
872 KB |
Output is correct |
45 |
Correct |
361 ms |
860 KB |
Output is correct |
46 |
Correct |
389 ms |
1096 KB |
Output is correct |
47 |
Correct |
356 ms |
904 KB |
Output is correct |
48 |
Correct |
355 ms |
880 KB |
Output is correct |
49 |
Correct |
375 ms |
852 KB |
Output is correct |
50 |
Correct |
380 ms |
1100 KB |
Output is correct |
51 |
Correct |
320 ms |
848 KB |
Output is correct |
52 |
Correct |
271 ms |
2856 KB |
Output is correct |
53 |
Correct |
225 ms |
812 KB |
Output is correct |
54 |
Correct |
198 ms |
848 KB |
Output is correct |
55 |
Correct |
202 ms |
3068 KB |
Output is correct |
56 |
Correct |
240 ms |
2900 KB |
Output is correct |
57 |
Correct |
295 ms |
848 KB |
Output is correct |
58 |
Correct |
275 ms |
844 KB |
Output is correct |
59 |
Correct |
275 ms |
852 KB |
Output is correct |
60 |
Correct |
279 ms |
844 KB |
Output is correct |
61 |
Correct |
257 ms |
1060 KB |
Output is correct |
62 |
Correct |
284 ms |
1104 KB |
Output is correct |
63 |
Correct |
286 ms |
868 KB |
Output is correct |
64 |
Correct |
265 ms |
860 KB |
Output is correct |
65 |
Correct |
22 ms |
6200 KB |
Output is correct |
66 |
Execution timed out |
1016 ms |
34756 KB |
Time limit exceeded |
67 |
Halted |
0 ms |
0 KB |
- |