Submission #622634

# Submission time Handle Problem Language Result Execution time Memory
622634 2022-08-04T12:50:22 Z KoD Ancient Books (IOI17_books) C++17
100 / 100
152 ms 23788 KB
#include "books.h"
#include <bits/stdc++.h>

using ll = long long;
using std::pair;
using std::vector;

ll minimum_walk(vector<int> P, int s) {
    {
        int l = 0, r = (int)P.size() - 1;
        while (l < s and P[l] == l) {
            l += 1;
        }
        while (r > s and P[r] == r) {
            r -= 1;
        }
        P.erase(P.begin() + r + 1, P.end());
        P.erase(P.begin(), P.begin() + l);
        for (auto& x : P) {
            x -= l;
        }
        s -= l;
    }
    const int N = (int)P.size();
    vector<char> seen(N);
    vector<int> left(N), right(N);
    ll ans = 0;
    for (int i = 0; i < N; ++i) {
        ans += std::abs(P[i] - i);
        if (seen[i]) {
            continue;
        }
        vector<int> cycle;
        for (int j = i; !seen[j]; j = P[j]) {
            seen[j] = true;
            cycle.push_back(j);
        }
        const int l = *std::min_element(cycle.begin(), cycle.end());
        const int r = *std::max_element(cycle.begin(), cycle.end());
        for (const int j : cycle) {
            left[j] = l;
            right[j] = r;
        }
    }
    const auto extend = [&](int& l, int& r) {
        int a = std::min(left[l], left[r]), b = std::max(right[l], right[r]);
        while (a < l or r < b) {
            if (a < l) {
                l -= 1;
                a = std::min(a, left[l]);
                b = std::max(b, right[l]);
            } else {
                r += 1;
                a = std::min(a, left[r]);
                b = std::max(b, right[r]);
            }
        }
    };
    int l = s, r = s;
    while (true) {
        extend(l, r);
        int xl = l, xr = r, xc = 0;
        while (0 < xl and xr <= r) {
            xl -= 1;
            xc += 2;
            extend(xl, xr);
        }
        int yl = l, yr = r, yc = 0;
        while (l <= yl and yr < N - 1) {
            yr += 1;
            yc += 2;
            extend(yl, yr);
        }
        assert(!((xr > r) ^ (yl < l)));
        if (xr <= r) {
            ans += xc + yc;
            break;
        }
        ans += std::min(xc, yc);
        l = std::min(xl, yl);
        r = std::max(xr, yr);
    }
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 224 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 224 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 224 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 133 ms 21092 KB Output is correct
31 Correct 152 ms 19048 KB Output is correct
32 Correct 79 ms 8012 KB Output is correct
33 Correct 152 ms 16884 KB Output is correct
34 Correct 118 ms 16888 KB Output is correct
35 Correct 119 ms 16876 KB Output is correct
36 Correct 116 ms 16928 KB Output is correct
37 Correct 103 ms 16948 KB Output is correct
38 Correct 98 ms 16884 KB Output is correct
39 Correct 97 ms 16952 KB Output is correct
40 Correct 101 ms 17196 KB Output is correct
41 Correct 120 ms 20016 KB Output is correct
42 Correct 115 ms 18468 KB Output is correct
43 Correct 116 ms 16872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 300 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 224 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 133 ms 21092 KB Output is correct
31 Correct 152 ms 19048 KB Output is correct
32 Correct 79 ms 8012 KB Output is correct
33 Correct 152 ms 16884 KB Output is correct
34 Correct 118 ms 16888 KB Output is correct
35 Correct 119 ms 16876 KB Output is correct
36 Correct 116 ms 16928 KB Output is correct
37 Correct 103 ms 16948 KB Output is correct
38 Correct 98 ms 16884 KB Output is correct
39 Correct 97 ms 16952 KB Output is correct
40 Correct 101 ms 17196 KB Output is correct
41 Correct 120 ms 20016 KB Output is correct
42 Correct 115 ms 18468 KB Output is correct
43 Correct 116 ms 16872 KB Output is correct
44 Correct 0 ms 212 KB Output is correct
45 Correct 0 ms 212 KB Output is correct
46 Correct 0 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 0 ms 212 KB Output is correct
50 Correct 1 ms 212 KB Output is correct
51 Correct 0 ms 212 KB Output is correct
52 Correct 0 ms 212 KB Output is correct
53 Correct 1 ms 212 KB Output is correct
54 Correct 0 ms 212 KB Output is correct
55 Correct 0 ms 212 KB Output is correct
56 Correct 1 ms 212 KB Output is correct
57 Correct 1 ms 212 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 1 ms 300 KB Output is correct
61 Correct 1 ms 212 KB Output is correct
62 Correct 1 ms 212 KB Output is correct
63 Correct 1 ms 308 KB Output is correct
64 Correct 118 ms 23684 KB Output is correct
65 Correct 118 ms 23676 KB Output is correct
66 Correct 104 ms 23628 KB Output is correct
67 Correct 105 ms 23652 KB Output is correct
68 Correct 11 ms 2516 KB Output is correct
69 Correct 11 ms 2508 KB Output is correct
70 Correct 11 ms 2516 KB Output is correct
71 Correct 12 ms 2516 KB Output is correct
72 Correct 12 ms 2480 KB Output is correct
73 Correct 11 ms 2608 KB Output is correct
74 Correct 128 ms 23788 KB Output is correct
75 Correct 122 ms 23676 KB Output is correct
76 Correct 121 ms 23660 KB Output is correct
77 Correct 125 ms 23696 KB Output is correct
78 Correct 122 ms 23680 KB Output is correct
79 Correct 120 ms 23660 KB Output is correct
80 Correct 81 ms 14812 KB Output is correct
81 Correct 114 ms 23676 KB Output is correct
82 Correct 112 ms 23656 KB Output is correct
83 Correct 128 ms 23656 KB Output is correct
84 Correct 122 ms 23652 KB Output is correct
85 Correct 113 ms 23656 KB Output is correct
86 Correct 106 ms 23656 KB Output is correct
87 Correct 130 ms 23684 KB Output is correct
88 Correct 128 ms 23660 KB Output is correct
89 Correct 129 ms 23628 KB Output is correct
90 Correct 101 ms 23628 KB Output is correct
91 Correct 109 ms 23652 KB Output is correct
92 Correct 111 ms 23656 KB Output is correct