Submission #1011932

# Submission time Handle Problem Language Result Execution time Memory
1011932 2024-07-01T11:16:34 Z c2zi6 Ancient Books (IOI17_books) C++14
100 / 100
105 ms 20140 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 = br;
    }
    return ans;
}


# Verdict Execution time Memory 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 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 600 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 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 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 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 600 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 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 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 344 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory 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 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 600 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 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 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 344 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 105 ms 12164 KB Output is correct
31 Correct 88 ms 12112 KB Output is correct
32 Correct 70 ms 20060 KB Output is correct
33 Correct 80 ms 18516 KB Output is correct
34 Correct 84 ms 18360 KB Output is correct
35 Correct 87 ms 18512 KB Output is correct
36 Correct 87 ms 14188 KB Output is correct
37 Correct 79 ms 12528 KB Output is correct
38 Correct 70 ms 12116 KB Output is correct
39 Correct 69 ms 12112 KB Output is correct
40 Correct 88 ms 12012 KB Output is correct
41 Correct 83 ms 12012 KB Output is correct
42 Correct 85 ms 12112 KB Output is correct
43 Correct 75 ms 18356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 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 0 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 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 428 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 1 ms 348 KB Output is correct
# Verdict Execution time Memory 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 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 600 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 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 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 344 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 105 ms 12164 KB Output is correct
31 Correct 88 ms 12112 KB Output is correct
32 Correct 70 ms 20060 KB Output is correct
33 Correct 80 ms 18516 KB Output is correct
34 Correct 84 ms 18360 KB Output is correct
35 Correct 87 ms 18512 KB Output is correct
36 Correct 87 ms 14188 KB Output is correct
37 Correct 79 ms 12528 KB Output is correct
38 Correct 70 ms 12116 KB Output is correct
39 Correct 69 ms 12112 KB Output is correct
40 Correct 88 ms 12012 KB Output is correct
41 Correct 83 ms 12012 KB Output is correct
42 Correct 85 ms 12112 KB Output is correct
43 Correct 75 ms 18356 KB Output is correct
44 Correct 1 ms 344 KB Output is correct
45 Correct 0 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 0 ms 348 KB Output is correct
49 Correct 0 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 0 ms 348 KB Output is correct
53 Correct 0 ms 348 KB Output is correct
54 Correct 0 ms 348 KB Output is correct
55 Correct 0 ms 348 KB Output is correct
56 Correct 0 ms 348 KB Output is correct
57 Correct 0 ms 348 KB Output is correct
58 Correct 0 ms 428 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 0 ms 348 KB Output is correct
62 Correct 0 ms 348 KB Output is correct
63 Correct 1 ms 348 KB Output is correct
64 Correct 81 ms 18516 KB Output is correct
65 Correct 84 ms 18512 KB Output is correct
66 Correct 71 ms 12624 KB Output is correct
67 Correct 74 ms 12368 KB Output is correct
68 Correct 8 ms 1880 KB Output is correct
69 Correct 7 ms 1604 KB Output is correct
70 Correct 7 ms 2008 KB Output is correct
71 Correct 8 ms 2520 KB Output is correct
72 Correct 8 ms 2516 KB Output is correct
73 Correct 8 ms 1624 KB Output is correct
74 Correct 75 ms 18360 KB Output is correct
75 Correct 98 ms 18532 KB Output is correct
76 Correct 74 ms 18360 KB Output is correct
77 Correct 71 ms 18356 KB Output is correct
78 Correct 81 ms 18532 KB Output is correct
79 Correct 97 ms 18360 KB Output is correct
80 Correct 67 ms 20140 KB Output is correct
81 Correct 71 ms 16056 KB Output is correct
82 Correct 67 ms 16056 KB Output is correct
83 Correct 78 ms 15464 KB Output is correct
84 Correct 85 ms 15456 KB Output is correct
85 Correct 69 ms 13932 KB Output is correct
86 Correct 70 ms 12664 KB Output is correct
87 Correct 82 ms 18784 KB Output is correct
88 Correct 84 ms 18532 KB Output is correct
89 Correct 75 ms 15800 KB Output is correct
90 Correct 69 ms 12368 KB Output is correct
91 Correct 73 ms 12012 KB Output is correct
92 Correct 72 ms 12112 KB Output is correct