답안 #400799

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
400799 2021-05-08T16:49:07 Z idk321 고대 책들 (IOI17_books) C++11
50 / 100
338 ms 106164 KB
#include "books.h"
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int N = 1000000;

bool vis[N];
/*
bool tree[4 * N];

void push(int node)
{
    if (tree[node])
    {
        tree[node * 2] = true;
        tree[node * 2 +1] = true;
    }
}

void ins(int from, int to, int a, int b, int node)
{
    if (from <= a && b <= to)
    {
        tree[node] = true;
        return;
    }

    push(node);
    int mid = (a + b) / 2;
    if (from <= mid) ins(from, to, a, mid, node * 2);
    if (to > mid) ins(from, to, mid + 1, b, node * 2 + 1);
}

bool getIn(int i, int a, int b, int node)
{
    if (a == b) return tree[node];

    push(node);
    int mid = (a + b) / 2;
    if (i <= mid) return getIn(i, a, mid, node * 2);
    return getIn(i, mid + 1, b, node * 2 + 1);
} */

long long minimum_walk(std::vector<int> p, int s) {

    ll res = 0;

    int n = p.size();

    vector<array<int, 2>> ivals;
    vector<array<int, 2>> landr(n);
    vector<int> comp(n);
    int compNum = 0;
    for (int i = 0; i < n; i++)
    {
        if (p[i] == i || vis[i]) continue;
        int cur = i;
        int l = cur;
        int r = cur;
        do
        {
            vis[cur] = true;
            l = min(l, cur);
            r = max(r, cur);
            res += abs(p[cur] - cur);
            //ins(min(cur, p[cur]), max(cur, p[cur]), 0, n - 1, 1);
            cur = p[cur];
        } while (cur != i);
        ivals.push_back({l, r});

        do
        {

            comp[cur] = compNum;

            //ins(min(cur, p[cur]), max(cur, p[cur]), 0, n - 1, 1);
            cur = p[cur];
        } while (cur != i);

        landr[compNum][0] = l;
        landr[compNum][1] = r;
        compNum++;
    }


    if (res == 0) return 0;
    sort(ivals.begin(), ivals.end());

    vector<array<int, 2>> together;

    int l = ivals[0][0];
    int r = ivals[0][1];
    for (int i = 1; i < ivals.size(); i++)
    {
        if (ivals[i][0] > r)
        {
            together.push_back({l, r});
            l = ivals[i][0];
            r = ivals[i][1];
        } else
        {
            r = max(ivals[i][1], r);
        }
    }
    together.push_back({l, r});




    for (int i = 1; i < together.size(); i++)
    {
        res += (together[i][0] - together[i - 1][1]) * 2;
    }

    if (s  < together[0][0])
    {
        res += (together[0][0] - s) * 2;
    }
    if (s > together[together.size() - 1][1])
    {
        res += (s - together[together.size() - 1][1]) * 2;
    }

    int in = -1;
    for (int i = 0; i < together.size(); i++)
    {
        if (s >= together[i][0] && s <= together[i][1])
        {
            in = i;
            break;
        }
    }


    if (in != -1)
    {
        vector<int> dist(n, -1);
        int cr = 0;
        int cl = 0;
        int cur = s;
        deque<array<int, 2>> que;
        que.push_back({comp[s], 0});
        while (!que.empty())
        {
            auto cur = que.front();
            que.pop_front();
            if (dist[cur[0]] != -1) continue;
            dist[cur[0]] = cur[1];


            for (int i = landr[cur[0]][0]; i <= landr[cur[0]][1]; i++)
            {
                que.push_front({comp[i], cur[1]});
            }
            if (landr[cur[0]][0] - 1 >= together[in][0])
            {
                que.push_back({comp[landr[cur[0]][0] - 1], cur[1] + 1});
            }
            if (landr[cur[0]][1] + 1 <= together[in][1])
            {
                que.push_back({comp[landr[cur[0]][1] + 1], cur[1] + 1});
            }
        }

        res += min(dist[comp[together[in][0]]], dist[comp[together[in][1]]]);
    }


    return res;
}

/*
4 0
0 2 3 1
*/

/*
5 2
0 4 3 2 1
*/


Compilation message

books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:95:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |     for (int i = 1; i < ivals.size(); i++)
      |                     ~~^~~~~~~~~~~~~~
books.cpp:112:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  112 |     for (int i = 1; i < together.size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~
books.cpp:127:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  127 |     for (int i = 0; i < together.size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~
books.cpp:140:13: warning: unused variable 'cr' [-Wunused-variable]
  140 |         int cr = 0;
      |             ^~
books.cpp:141:13: warning: unused variable 'cl' [-Wunused-variable]
  141 |         int cl = 0;
      |             ^~
books.cpp:142:13: warning: unused variable 'cur' [-Wunused-variable]
  142 |         int cur = s;
      |             ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 2 ms 204 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 2 ms 204 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 298 ms 73596 KB Output is correct
31 Correct 332 ms 106164 KB Output is correct
32 Correct 130 ms 19832 KB Output is correct
33 Correct 151 ms 25396 KB Output is correct
34 Correct 153 ms 25396 KB Output is correct
35 Correct 166 ms 27960 KB Output is correct
36 Correct 153 ms 27628 KB Output is correct
37 Correct 140 ms 25064 KB Output is correct
38 Correct 146 ms 24828 KB Output is correct
39 Correct 144 ms 24772 KB Output is correct
40 Correct 153 ms 25084 KB Output is correct
41 Correct 207 ms 45696 KB Output is correct
42 Correct 192 ms 24772 KB Output is correct
43 Correct 338 ms 65596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 332 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 2 ms 204 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 298 ms 73596 KB Output is correct
31 Correct 332 ms 106164 KB Output is correct
32 Correct 130 ms 19832 KB Output is correct
33 Correct 151 ms 25396 KB Output is correct
34 Correct 153 ms 25396 KB Output is correct
35 Correct 166 ms 27960 KB Output is correct
36 Correct 153 ms 27628 KB Output is correct
37 Correct 140 ms 25064 KB Output is correct
38 Correct 146 ms 24828 KB Output is correct
39 Correct 144 ms 24772 KB Output is correct
40 Correct 153 ms 25084 KB Output is correct
41 Correct 207 ms 45696 KB Output is correct
42 Correct 192 ms 24772 KB Output is correct
43 Correct 338 ms 65596 KB Output is correct
44 Incorrect 1 ms 332 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
45 Halted 0 ms 0 KB -