Submission #1044483

# Submission time Handle Problem Language Result Execution time Memory
1044483 2024-08-05T10:08:02 Z totoro Ancient Books (IOI17_books) C++17
50 / 100
292 ms 17096 KB
// SOLVED SUBTASK 1   (12 pts)
// SOLVED SUBTASK 2   (10 pts)
// SOLVED SUBTASK 3   (28 pts)
// UNSOLVED SUBTASK 4 (20 pts)
// UNSOLVED SUBTASK 5 (30 pts)
// [+-+]----------------------
// TOTAL      50/100 pts

#include "books.h"

#include <cmath>

struct Component {
    int start;
    int end;
    Component(int start, int end) : start(start), end(end) {}
};

long long sum_cycles(Component& component, std::vector<int>& p) {
    std::vector<bool> visited(p.size(), false);

    long long sum = 0;
    for (int i = component.start; i <= component.end; ++i) {
        int current = i;
        while (!visited[current]) {
            visited[current] = true;
            long long diff = std::abs(current - p[current]);
            sum += diff;
            current = p[current];
        }
    }

    return sum;
}

long long minimum_walk(std::vector<int> p, int s) {
    bool already_sorted = true;
    size_t first_unsolved = 0;
    for (size_t i = 0; i < p.size(); ++i) {
        if (i != p[i]) {
            already_sorted = false;
            first_unsolved = i;
            break;
        }
    }
    if (already_sorted) {
        return 0;
    }

    size_t last_unsolved = p.size() - 1;
    for (int i = p.size() - 1; i >= 0; --i) {
        if (i != p[i]) {
            last_unsolved = i;
            break;
        }
    }

    std::vector<Component> components;

    int block_start = 0;
    int block_end = 0;
    bool in_block = false;
    size_t distances = 0;
    for (size_t i = first_unsolved; i <= last_unsolved; ++i) {
        if (i == p[i] && !in_block) {
            ++distances;
            continue;
        }
        if (!in_block) {
            block_start = i;
            block_end = p[i];
            in_block = true;
        } else {
            block_end = std::max(block_end, p[i]);
        }
        if (i == block_end) {
            components.emplace_back(block_start, block_end);
            in_block = false;
        }
    }

    size_t final_distances = 2 * (distances + components.size() - 1);
    if (s < first_unsolved) {
        final_distances += 2 * (first_unsolved - s);
    } else if (s > last_unsolved) {
        final_distances += 2 * (s - last_unsolved);
    }

    size_t sum = final_distances;
    for (auto& component : components) {
        sum += sum_cycles(component, p);
    }

    return sum;
}

Compilation message

books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:40:15: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} [-Wsign-compare]
   40 |         if (i != p[i]) {
books.cpp:65:15: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} [-Wsign-compare]
   65 |         if (i == p[i] && !in_block) {
books.cpp:76:15: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   76 |         if (i == block_end) {
      |             ~~^~~~~~~~~~~~
books.cpp:83:11: warning: comparison of integer expressions of different signedness: 'int' and 'size_t' {aka 'long unsigned int'} [-Wsign-compare]
   83 |     if (s < first_unsolved) {
      |         ~~^~~~~~~~~~~~~~~~
books.cpp:85:18: warning: comparison of integer expressions of different signedness: 'int' and 'size_t' {aka 'long unsigned int'} [-Wsign-compare]
   85 |     } else if (s > last_unsolved) {
      |                ~~^~~~~~~~~~~~~~~
# 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 348 KB Output is correct
7 Correct 0 ms 392 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 424 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 424 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 348 KB Output is correct
7 Correct 0 ms 392 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 424 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 424 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 344 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 0 ms 344 KB Output is correct
27 Correct 0 ms 600 KB Output is correct
28 Correct 0 ms 348 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 348 KB Output is correct
7 Correct 0 ms 392 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 424 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 424 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 344 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 0 ms 344 KB Output is correct
27 Correct 0 ms 600 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 71 ms 14928 KB Output is correct
31 Correct 72 ms 14948 KB Output is correct
32 Correct 56 ms 14932 KB Output is correct
33 Correct 292 ms 17096 KB Output is correct
34 Correct 286 ms 16968 KB Output is correct
35 Correct 291 ms 16984 KB Output is correct
36 Correct 181 ms 16072 KB Output is correct
37 Correct 98 ms 15184 KB Output is correct
38 Correct 64 ms 14928 KB Output is correct
39 Correct 63 ms 14928 KB Output is correct
40 Correct 77 ms 15012 KB Output is correct
41 Correct 74 ms 15060 KB Output is correct
42 Correct 70 ms 14928 KB Output is correct
43 Correct 68 ms 14948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 348 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
2 Halted 0 ms 0 KB -
# 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 348 KB Output is correct
7 Correct 0 ms 392 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 424 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 424 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 344 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 0 ms 344 KB Output is correct
27 Correct 0 ms 600 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 71 ms 14928 KB Output is correct
31 Correct 72 ms 14948 KB Output is correct
32 Correct 56 ms 14932 KB Output is correct
33 Correct 292 ms 17096 KB Output is correct
34 Correct 286 ms 16968 KB Output is correct
35 Correct 291 ms 16984 KB Output is correct
36 Correct 181 ms 16072 KB Output is correct
37 Correct 98 ms 15184 KB Output is correct
38 Correct 64 ms 14928 KB Output is correct
39 Correct 63 ms 14928 KB Output is correct
40 Correct 77 ms 15012 KB Output is correct
41 Correct 74 ms 15060 KB Output is correct
42 Correct 70 ms 14928 KB Output is correct
43 Correct 68 ms 14948 KB Output is correct
44 Incorrect 0 ms 348 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
45 Halted 0 ms 0 KB -