답안 #831757

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
831757 2023-08-20T13:24:48 Z finn__ 고대 책들 (IOI17_books) C++17
50 / 100
1408 ms 55088 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())
            {
                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)
      |                     ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 256 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 1 ms 212 KB Output is correct
9 Correct 1 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 1 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 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 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 256 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 1 ms 212 KB Output is correct
9 Correct 1 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 1 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 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 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 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 0 ms 340 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 360 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 256 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 1 ms 212 KB Output is correct
9 Correct 1 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 1 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 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 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 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 0 ms 340 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 360 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1355 ms 55088 KB Output is correct
31 Correct 1408 ms 55088 KB Output is correct
32 Correct 99 ms 8048 KB Output is correct
33 Correct 229 ms 31624 KB Output is correct
34 Correct 235 ms 31636 KB Output is correct
35 Correct 278 ms 39460 KB Output is correct
36 Correct 366 ms 50364 KB Output is correct
37 Correct 444 ms 54680 KB Output is correct
38 Correct 471 ms 55036 KB Output is correct
39 Correct 452 ms 55044 KB Output is correct
40 Correct 500 ms 55084 KB Output is correct
41 Correct 875 ms 55088 KB Output is correct
42 Correct 699 ms 55080 KB Output is correct
43 Correct 258 ms 31944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 212 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3506'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 256 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 1 ms 212 KB Output is correct
9 Correct 1 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 1 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 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 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 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 0 ms 340 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 360 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1355 ms 55088 KB Output is correct
31 Correct 1408 ms 55088 KB Output is correct
32 Correct 99 ms 8048 KB Output is correct
33 Correct 229 ms 31624 KB Output is correct
34 Correct 235 ms 31636 KB Output is correct
35 Correct 278 ms 39460 KB Output is correct
36 Correct 366 ms 50364 KB Output is correct
37 Correct 444 ms 54680 KB Output is correct
38 Correct 471 ms 55036 KB Output is correct
39 Correct 452 ms 55044 KB Output is correct
40 Correct 500 ms 55084 KB Output is correct
41 Correct 875 ms 55088 KB Output is correct
42 Correct 699 ms 55080 KB Output is correct
43 Correct 258 ms 31944 KB Output is correct
44 Incorrect 1 ms 212 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3506'
45 Halted 0 ms 0 KB -