Submission #831807

# Submission time Handle Problem Language Result Execution time Memory
831807 2023-08-20T15:34:43 Z finn__ Ancient Books (IOI17_books) C++17
100 / 100
1471 ms 69708 KB
#include "books.h"
#include <bits/stdc++.h>
using namespace std;
using L = long long;

long long minimum_walk(std::vector<int> p, int s)
{
    int const n = p.size();
    L ans = 0;
    for (int i = 0; i < n; ++i)
        ans += abs(i - p[i]);

    vector<pair<int, int>> cycle(n, {-1, -1});
    for (int i = 0; i < n; ++i)
        if (cycle[i].first == -1)
        {
            int x = i, a = n, b = -1;
            do
            {
                a = min(a, x);
                b = max(b, x);
                x = p[x];
            } while (x != i);
            do
            {
                cycle[x] = {a, b};
                x = p[x];
            } while (x != i);
        }

    set<int> unexplored;
    for (int i = 0; i < n; ++i)
        if (p[i] != i)
            unexplored.insert(i);
    int a = s, b = s;

    auto explore_cycle = [&](int u)
    {
        auto it = unexplored.find(u);
        while (it != unexplored.end())
        {
            a = min(a, u);
            b = max(b, u);
            unexplored.erase(it);
            u = p[u];
            it = unexplored.find(u);
        }
    };

    explore_cycle(s);

    int first_non_id = 0, last_non_id = n - 1;
    while (first_non_id < s && p[first_non_id] == first_non_id)
        ++first_non_id;
    while (last_non_id > s && p[last_non_id] == last_non_id)
        --last_non_id;

    while (!unexplored.empty())
    {
        auto it = unexplored.lower_bound(a);
        if (it != unexplored.end() && *it <= b)
        {
            explore_cycle(*it);
        }
        else
        {
            int cost_left = 0, cost_right = 0, component_end = s;

            int i = a;
            while (i >= first_non_id)
            {
                if (cycle[i].second > b)
                    break;
                component_end = min(component_end, min(i, p[i]));
                if (i == component_end && i != first_non_id)
                    cost_left += 2;
                --i;
            }

            int j = i;

            i = b;
            component_end = s;
            while (i <= last_non_id)
            {
                if (cycle[i].first < a)
                    break;
                component_end = max(component_end, max(i, p[i]));
                if (i == component_end && i != last_non_id)
                    cost_right += 2;
                ++i;
            }

            if (i == last_non_id + 1)
                return ans + cost_left + cost_right;
            if (cost_left < cost_right)
            {
                ans += cost_left;
                a = j;
            }
            else
            {
                ans += cost_right;
                b = i;
            }
        }
    }

    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 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 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 0 ms 216 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 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 0 ms 216 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 356 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 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 0 ms 216 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 356 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1455 ms 62916 KB Output is correct
31 Correct 1471 ms 62916 KB Output is correct
32 Correct 102 ms 15852 KB Output is correct
33 Correct 178 ms 39476 KB Output is correct
34 Correct 173 ms 39408 KB Output is correct
35 Correct 207 ms 47224 KB Output is correct
36 Correct 276 ms 58188 KB Output is correct
37 Correct 303 ms 62600 KB Output is correct
38 Correct 293 ms 62956 KB Output is correct
39 Correct 268 ms 62836 KB Output is correct
40 Correct 317 ms 62796 KB Output is correct
41 Correct 390 ms 62968 KB Output is correct
42 Correct 295 ms 62816 KB Output is correct
43 Correct 232 ms 39884 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 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 300 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 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 0 ms 216 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 356 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1455 ms 62916 KB Output is correct
31 Correct 1471 ms 62916 KB Output is correct
32 Correct 102 ms 15852 KB Output is correct
33 Correct 178 ms 39476 KB Output is correct
34 Correct 173 ms 39408 KB Output is correct
35 Correct 207 ms 47224 KB Output is correct
36 Correct 276 ms 58188 KB Output is correct
37 Correct 303 ms 62600 KB Output is correct
38 Correct 293 ms 62956 KB Output is correct
39 Correct 268 ms 62836 KB Output is correct
40 Correct 317 ms 62796 KB Output is correct
41 Correct 390 ms 62968 KB Output is correct
42 Correct 295 ms 62816 KB Output is correct
43 Correct 232 ms 39884 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 340 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 1 ms 340 KB Output is correct
50 Correct 1 ms 212 KB Output is correct
51 Correct 1 ms 340 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 1 ms 212 KB Output is correct
54 Correct 1 ms 340 KB Output is correct
55 Correct 1 ms 340 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 1 ms 340 KB Output is correct
58 Correct 1 ms 340 KB Output is correct
59 Correct 1 ms 340 KB Output is correct
60 Correct 1 ms 300 KB Output is correct
61 Correct 1 ms 340 KB Output is correct
62 Correct 1 ms 340 KB Output is correct
63 Correct 1 ms 340 KB Output is correct
64 Correct 186 ms 40412 KB Output is correct
65 Correct 162 ms 36208 KB Output is correct
66 Correct 389 ms 69096 KB Output is correct
67 Correct 397 ms 69352 KB Output is correct
68 Correct 27 ms 6396 KB Output is correct
69 Correct 30 ms 6768 KB Output is correct
70 Correct 25 ms 5924 KB Output is correct
71 Correct 18 ms 4708 KB Output is correct
72 Correct 16 ms 3584 KB Output is correct
73 Correct 38 ms 7092 KB Output is correct
74 Correct 175 ms 46188 KB Output is correct
75 Correct 177 ms 46076 KB Output is correct
76 Correct 235 ms 46828 KB Output is correct
77 Correct 231 ms 46576 KB Output is correct
78 Correct 294 ms 57216 KB Output is correct
79 Correct 305 ms 57224 KB Output is correct
80 Correct 81 ms 22628 KB Output is correct
81 Correct 399 ms 69608 KB Output is correct
82 Correct 400 ms 69676 KB Output is correct
83 Correct 361 ms 63308 KB Output is correct
84 Correct 388 ms 65160 KB Output is correct
85 Correct 391 ms 68108 KB Output is correct
86 Correct 398 ms 69300 KB Output is correct
87 Correct 167 ms 37104 KB Output is correct
88 Correct 268 ms 50512 KB Output is correct
89 Correct 357 ms 61032 KB Output is correct
90 Correct 405 ms 69452 KB Output is correct
91 Correct 416 ms 69552 KB Output is correct
92 Correct 434 ms 69708 KB Output is correct