답안 #831804

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
831804 2023-08-20T15:31:00 Z finn__ 고대 책들 (IOI17_books) C++17
50 / 100
2000 ms 63616 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 > s)
                    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 < s)
                    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;
}
# 결과 실행 시간 메모리 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 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 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 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 300 KB Output is correct
# 결과 실행 시간 메모리 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 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 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 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 300 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 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 300 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 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 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 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 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 300 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 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 300 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1276 ms 63432 KB Output is correct
31 Correct 1336 ms 63456 KB Output is correct
32 Correct 81 ms 16344 KB Output is correct
33 Correct 182 ms 40000 KB Output is correct
34 Correct 192 ms 39964 KB Output is correct
35 Correct 243 ms 47720 KB Output is correct
36 Correct 257 ms 58676 KB Output is correct
37 Correct 283 ms 63008 KB Output is correct
38 Correct 273 ms 63324 KB Output is correct
39 Correct 270 ms 63388 KB Output is correct
40 Correct 285 ms 63332 KB Output is correct
41 Correct 386 ms 63328 KB Output is correct
42 Correct 294 ms 63616 KB Output is correct
43 Correct 235 ms 40292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2064 ms 212 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 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 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 300 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 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 300 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1276 ms 63432 KB Output is correct
31 Correct 1336 ms 63456 KB Output is correct
32 Correct 81 ms 16344 KB Output is correct
33 Correct 182 ms 40000 KB Output is correct
34 Correct 192 ms 39964 KB Output is correct
35 Correct 243 ms 47720 KB Output is correct
36 Correct 257 ms 58676 KB Output is correct
37 Correct 283 ms 63008 KB Output is correct
38 Correct 273 ms 63324 KB Output is correct
39 Correct 270 ms 63388 KB Output is correct
40 Correct 285 ms 63332 KB Output is correct
41 Correct 386 ms 63328 KB Output is correct
42 Correct 294 ms 63616 KB Output is correct
43 Correct 235 ms 40292 KB Output is correct
44 Execution timed out 2064 ms 212 KB Time limit exceeded
45 Halted 0 ms 0 KB -