Submission #1011930

# Submission time Handle Problem Language Result Execution time Memory
1011930 2024-07-01T11:15:54 Z c2zi6 Ancient Books (IOI17_books) C++14
100 / 100
163 ms 26836 KB
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define replr(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define reprl(i, a, b) for (int i = int(a); i >= int(b); --i)
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define mkp(a, b) make_pair(a, b)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VPI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<VPI> VVPI;
typedef pair<ll, ll> PLL;
typedef vector<ll> VL;
typedef vector<PLL> VPL;
typedef vector<VL> VVL;
typedef vector<VVL> VVVL;
typedef vector<VPL> VVPL;
template<class T> T setmax(T& a, T b) {if (a < b) return a = b; return a;}
template<class T> T setmin(T& a, T b) {if (a < b) return a; return a = b;}
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T>
using indset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#include "books.h"

VI L, R, C;

void extend(int& l, int& r) {
    int ll = min(L[C[l]], L[C[r]]);
    int rr = max(R[C[l]], R[C[r]]);
    while (ll < l || r < rr) {
        if (ll < l) {
            l--;
            setmin(ll, L[C[l]]);
            setmax(rr, R[C[l]]);
        } else if (r < rr) {
            r++;
            setmin(ll, L[C[r]]);
            setmax(rr, R[C[r]]);
        }
    }
}

ll minimum_walk(VI p, int s) {
    int n = p.size();
    C = VI(n, -1);
    int c = 0;
    ll ans = 0;
    int entl = +2e9, entr = -2e9;
    rep(i, n) if (C[i] == -1) {
        int u = i;
        int l = +2e9, r = -2e9;
        do {
            C[u] = c;
            setmin(l, u);
            setmax(r, u);
            ans += abs(p[u]-u);
            u = p[u];
        } while (u != i);
        L.pb(l);
        R.pb(r);
        if (i != p[i]) {
            setmin(entl, l);
            setmax(entr, r);
        }
        c++;
    }

    int l = s;
    int r = s;
    while (true) {
        extend(l, r);
        int al = l;
        int ar = r;
        int ac = 0;
        while (ar < entr && al == l) {
            extend(al, ++ar);
            ac += 2;
        }
        int bl = l;
        int br = r;
        int bc = 0;
        while (bl > entl && br == r) {
            extend(--bl, br);
            bc += 2;
        }
        if (al == l) {
            ans += ac + bc;
            break;
        }
        ans += min(ac, bc);
        l = al;
        r = ar;
    }
    return ans;
}


# Verdict Execution time Memory Grader output
1 Correct 1 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 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 388 KB Output is correct
7 Correct 0 ms 600 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 436 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 352 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 388 KB Output is correct
7 Correct 0 ms 600 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 436 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 352 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 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 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 388 KB Output is correct
7 Correct 0 ms 600 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 436 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 352 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 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 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 107 ms 17312 KB Output is correct
31 Correct 101 ms 17364 KB Output is correct
32 Correct 84 ms 25604 KB Output is correct
33 Correct 88 ms 23748 KB Output is correct
34 Correct 96 ms 23904 KB Output is correct
35 Correct 91 ms 23912 KB Output is correct
36 Correct 86 ms 19576 KB Output is correct
37 Correct 95 ms 17972 KB Output is correct
38 Correct 80 ms 17492 KB Output is correct
39 Correct 114 ms 17488 KB Output is correct
40 Correct 82 ms 17500 KB Output is correct
41 Correct 163 ms 17360 KB Output is correct
42 Correct 96 ms 17440 KB Output is correct
43 Correct 126 ms 23740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 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 Correct 1 ms 348 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 1 ms 456 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 448 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 440 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 388 KB Output is correct
7 Correct 0 ms 600 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 436 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 352 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 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 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 107 ms 17312 KB Output is correct
31 Correct 101 ms 17364 KB Output is correct
32 Correct 84 ms 25604 KB Output is correct
33 Correct 88 ms 23748 KB Output is correct
34 Correct 96 ms 23904 KB Output is correct
35 Correct 91 ms 23912 KB Output is correct
36 Correct 86 ms 19576 KB Output is correct
37 Correct 95 ms 17972 KB Output is correct
38 Correct 80 ms 17492 KB Output is correct
39 Correct 114 ms 17488 KB Output is correct
40 Correct 82 ms 17500 KB Output is correct
41 Correct 163 ms 17360 KB Output is correct
42 Correct 96 ms 17440 KB Output is correct
43 Correct 126 ms 23740 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 0 ms 348 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 0 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 0 ms 348 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 1 ms 456 KB Output is correct
53 Correct 1 ms 348 KB Output is correct
54 Correct 1 ms 448 KB Output is correct
55 Correct 1 ms 348 KB Output is correct
56 Correct 0 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 440 KB Output is correct
62 Correct 1 ms 348 KB Output is correct
63 Correct 0 ms 348 KB Output is correct
64 Correct 133 ms 23916 KB Output is correct
65 Correct 102 ms 23972 KB Output is correct
66 Correct 82 ms 17920 KB Output is correct
67 Correct 120 ms 17764 KB Output is correct
68 Correct 9 ms 2484 KB Output is correct
69 Correct 9 ms 2360 KB Output is correct
70 Correct 14 ms 2652 KB Output is correct
71 Correct 8 ms 2992 KB Output is correct
72 Correct 12 ms 3116 KB Output is correct
73 Correct 8 ms 2196 KB Output is correct
74 Correct 98 ms 25156 KB Output is correct
75 Correct 96 ms 25088 KB Output is correct
76 Correct 93 ms 25168 KB Output is correct
77 Correct 92 ms 25308 KB Output is correct
78 Correct 125 ms 25172 KB Output is correct
79 Correct 100 ms 25180 KB Output is correct
80 Correct 84 ms 26836 KB Output is correct
81 Correct 82 ms 22872 KB Output is correct
82 Correct 104 ms 22876 KB Output is correct
83 Correct 90 ms 21980 KB Output is correct
84 Correct 96 ms 22112 KB Output is correct
85 Correct 93 ms 20680 KB Output is correct
86 Correct 91 ms 19432 KB Output is correct
87 Correct 96 ms 25684 KB Output is correct
88 Correct 95 ms 25300 KB Output is correct
89 Correct 90 ms 22612 KB Output is correct
90 Correct 88 ms 19192 KB Output is correct
91 Correct 86 ms 18920 KB Output is correct
92 Correct 87 ms 18800 KB Output is correct