Submission #831758

# Submission time Handle Problem Language Result Execution time Memory
831758 2023-08-20T13:30:20 Z finn__ Ancient Books (IOI17_books) C++17
50 / 100
1232 ms 55160 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)
{
    size_t const n = p.size();
    L ans = 0;
    for (int i = 0; i < n; ++i)
        ans += abs(i - p[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);
            u = p[u];
            unexplored.erase(it);
            it = unexplored.find(u);
        }
    };

    explore_cycle(s);
    while (!unexplored.empty())
    {
        auto it = unexplored.lower_bound(a);
        if (it != unexplored.end() && *it <= b)
        {
            explore_cycle(*it);
        }
        else
        {
            if (it != unexplored.end() && (it == unexplored.begin() || *it - b < a - *prev(it)))
            {
                ans += 2 * (*it - b);
                explore_cycle(*it);
            }
            else
            {
                --it;
                ans += 2 * (a - *it);
                explore_cycle(*it);
            }
        }
    }

    return ans;
}

Compilation message

books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:10:23: warning: comparison of integer expressions of different signedness: 'int' and 'const size_t' {aka 'const long unsigned int'} [-Wsign-compare]
   10 |     for (int i = 0; i < n; ++i)
      |                     ~~^~~
books.cpp:14:23: warning: comparison of integer expressions of different signedness: 'int' and 'const size_t' {aka 'const long unsigned int'} [-Wsign-compare]
   14 |     for (int i = 0; i < n; ++i)
      |                     ~~^~~
# 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 1 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 212 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 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 1 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 212 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 340 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 1 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 0 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 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 1 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 212 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 340 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 1 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 0 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 1232 ms 55160 KB Output is correct
31 Correct 1230 ms 55084 KB Output is correct
32 Correct 78 ms 8012 KB Output is correct
33 Correct 205 ms 31600 KB Output is correct
34 Correct 206 ms 31596 KB Output is correct
35 Correct 253 ms 39456 KB Output is correct
36 Correct 331 ms 50352 KB Output is correct
37 Correct 390 ms 54776 KB Output is correct
38 Correct 405 ms 55036 KB Output is correct
39 Correct 404 ms 55048 KB Output is correct
40 Correct 450 ms 54988 KB Output is correct
41 Correct 812 ms 55116 KB Output is correct
42 Correct 731 ms 55076 KB Output is correct
43 Correct 232 ms 31860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 212 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3324'
2 Halted 0 ms 0 KB -
# 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 1 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 212 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 340 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 1 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 0 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 1232 ms 55160 KB Output is correct
31 Correct 1230 ms 55084 KB Output is correct
32 Correct 78 ms 8012 KB Output is correct
33 Correct 205 ms 31600 KB Output is correct
34 Correct 206 ms 31596 KB Output is correct
35 Correct 253 ms 39456 KB Output is correct
36 Correct 331 ms 50352 KB Output is correct
37 Correct 390 ms 54776 KB Output is correct
38 Correct 405 ms 55036 KB Output is correct
39 Correct 404 ms 55048 KB Output is correct
40 Correct 450 ms 54988 KB Output is correct
41 Correct 812 ms 55116 KB Output is correct
42 Correct 731 ms 55076 KB Output is correct
43 Correct 232 ms 31860 KB Output is correct
44 Incorrect 1 ms 212 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3324'
45 Halted 0 ms 0 KB -