답안 #40337

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
40337 2018-01-31T05:11:19 Z jackyliuxx 고대 책들 (IOI17_books) C++14
100 / 100
315 ms 21528 KB
#include "books.h"
#include <bits/stdc++.h>
using namespace std;

vector<int> group;
vector<int> group_head, group_tail;

int find_group(int x) {
    return x == group[x] ? x : group[x] = find_group(group[x]);
}

void merge_group(int x, int y) {
    int gx = find_group(x), gy = find_group(y);
    group_head[gx] = min(group_head[gx], group_head[gy]);
    group_tail[gx] = max(group_tail[gx], group_tail[gy]);
    group[gy] = gx;
}

bool is_head(int x) {
    return group_head[find_group(x)] == x;
}

bool is_tail(int x) {
    return group_tail[find_group(x)] == x;
}

long long minimum_walk(std::vector<int> p, int s) {
    long long ans = 0;
    int n = p.size();

    group.assign(n, 0);
    group_head.assign(n, 0);
    group_tail.assign(n, 0);

    for (int i = 0; i < n; i++) {
        group[i] = i;
        group_head[i] = i;
        group_tail[i] = i;
    }

    for (int i = 0; i < n; i++) {
        merge_group(p[i], i);
        ans += abs(p[i] - i);
    }

    stack<int> stk;
    for (int i = 0; i < n; i++) {
        if (is_head(i)) {
            stk.push(i);
        } else {
            while (find_group(stk.top()) != find_group(i)) {
                merge_group(stk.top(), i);
                stk.pop();
            }
        }
        if (is_tail(i)) {
            stk.pop();
        }
    }

    int target_head = 0, target_tail = n - 1;
    for (int i = 0; i < n && p[i] == i; i++) {
        target_head++;
    }
    for (int i = n - 1; i > 0 && p[i] == i; i--) {
        target_tail--;
    }

    int g = find_group(s);
    int head = group_head[g], tail = group_tail[g];
    while (head > target_head || tail < target_tail) {
        int l = head, ld = 0;
        while (l > target_head) {
            l--;
            ld++;
            int lg = find_group(l);
            l = group_head[lg];
            if (group_tail[lg] > tail) {
                break;
            }
        }
        int r = tail, rd = 0;
        while (r < target_tail) {
            r++;
            rd++;
            int rg = find_group(r);
            r = group_tail[rg];
            if (group_head[rg] < head) {
                break;
            }
        }
        if (find_group(l) == find_group(r)) {
            g = find_group(l);
            head = group_head[g];
            tail = group_tail[g];
            ans += min(ld, rd) * 2;
        } else {
            head = l;
            tail = r;
            ans += ld * 2;
            ans += rd * 2;
        }
    }

	return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 248 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 520 KB Output is correct
4 Correct 2 ms 520 KB Output is correct
5 Correct 1 ms 520 KB Output is correct
6 Correct 1 ms 520 KB Output is correct
7 Correct 2 ms 520 KB Output is correct
8 Correct 1 ms 648 KB Output is correct
9 Correct 2 ms 648 KB Output is correct
10 Correct 2 ms 648 KB Output is correct
11 Correct 1 ms 648 KB Output is correct
12 Correct 1 ms 648 KB Output is correct
13 Correct 1 ms 648 KB Output is correct
14 Correct 1 ms 648 KB Output is correct
15 Correct 1 ms 648 KB Output is correct
16 Correct 1 ms 648 KB Output is correct
17 Correct 1 ms 648 KB Output is correct
18 Correct 1 ms 648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 248 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 520 KB Output is correct
4 Correct 2 ms 520 KB Output is correct
5 Correct 1 ms 520 KB Output is correct
6 Correct 1 ms 520 KB Output is correct
7 Correct 2 ms 520 KB Output is correct
8 Correct 1 ms 648 KB Output is correct
9 Correct 2 ms 648 KB Output is correct
10 Correct 2 ms 648 KB Output is correct
11 Correct 1 ms 648 KB Output is correct
12 Correct 1 ms 648 KB Output is correct
13 Correct 1 ms 648 KB Output is correct
14 Correct 1 ms 648 KB Output is correct
15 Correct 1 ms 648 KB Output is correct
16 Correct 1 ms 648 KB Output is correct
17 Correct 1 ms 648 KB Output is correct
18 Correct 1 ms 648 KB Output is correct
19 Correct 2 ms 648 KB Output is correct
20 Correct 2 ms 648 KB Output is correct
21 Correct 1 ms 648 KB Output is correct
22 Correct 2 ms 648 KB Output is correct
23 Correct 2 ms 648 KB Output is correct
24 Correct 2 ms 648 KB Output is correct
25 Correct 2 ms 648 KB Output is correct
26 Correct 2 ms 648 KB Output is correct
27 Correct 2 ms 648 KB Output is correct
28 Correct 2 ms 648 KB Output is correct
29 Correct 3 ms 648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 248 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 520 KB Output is correct
4 Correct 2 ms 520 KB Output is correct
5 Correct 1 ms 520 KB Output is correct
6 Correct 1 ms 520 KB Output is correct
7 Correct 2 ms 520 KB Output is correct
8 Correct 1 ms 648 KB Output is correct
9 Correct 2 ms 648 KB Output is correct
10 Correct 2 ms 648 KB Output is correct
11 Correct 1 ms 648 KB Output is correct
12 Correct 1 ms 648 KB Output is correct
13 Correct 1 ms 648 KB Output is correct
14 Correct 1 ms 648 KB Output is correct
15 Correct 1 ms 648 KB Output is correct
16 Correct 1 ms 648 KB Output is correct
17 Correct 1 ms 648 KB Output is correct
18 Correct 1 ms 648 KB Output is correct
19 Correct 2 ms 648 KB Output is correct
20 Correct 2 ms 648 KB Output is correct
21 Correct 1 ms 648 KB Output is correct
22 Correct 2 ms 648 KB Output is correct
23 Correct 2 ms 648 KB Output is correct
24 Correct 2 ms 648 KB Output is correct
25 Correct 2 ms 648 KB Output is correct
26 Correct 2 ms 648 KB Output is correct
27 Correct 2 ms 648 KB Output is correct
28 Correct 2 ms 648 KB Output is correct
29 Correct 3 ms 648 KB Output is correct
30 Correct 315 ms 20116 KB Output is correct
31 Correct 305 ms 20172 KB Output is correct
32 Correct 173 ms 20172 KB Output is correct
33 Correct 176 ms 20172 KB Output is correct
34 Correct 180 ms 20172 KB Output is correct
35 Correct 186 ms 20172 KB Output is correct
36 Correct 219 ms 20172 KB Output is correct
37 Correct 198 ms 20172 KB Output is correct
38 Correct 208 ms 20172 KB Output is correct
39 Correct 212 ms 20172 KB Output is correct
40 Correct 209 ms 20172 KB Output is correct
41 Correct 239 ms 20172 KB Output is correct
42 Correct 241 ms 20172 KB Output is correct
43 Correct 182 ms 20172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 20172 KB Output is correct
2 Correct 1 ms 20172 KB Output is correct
3 Correct 2 ms 20172 KB Output is correct
4 Correct 2 ms 20172 KB Output is correct
5 Correct 2 ms 20172 KB Output is correct
6 Correct 2 ms 20172 KB Output is correct
7 Correct 2 ms 20172 KB Output is correct
8 Correct 2 ms 20172 KB Output is correct
9 Correct 2 ms 20172 KB Output is correct
10 Correct 2 ms 20172 KB Output is correct
11 Correct 2 ms 20172 KB Output is correct
12 Correct 2 ms 20172 KB Output is correct
13 Correct 2 ms 20172 KB Output is correct
14 Correct 2 ms 20172 KB Output is correct
15 Correct 2 ms 20172 KB Output is correct
16 Correct 2 ms 20172 KB Output is correct
17 Correct 2 ms 20172 KB Output is correct
18 Correct 2 ms 20172 KB Output is correct
19 Correct 2 ms 20172 KB Output is correct
20 Correct 2 ms 20172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 248 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 520 KB Output is correct
4 Correct 2 ms 520 KB Output is correct
5 Correct 1 ms 520 KB Output is correct
6 Correct 1 ms 520 KB Output is correct
7 Correct 2 ms 520 KB Output is correct
8 Correct 1 ms 648 KB Output is correct
9 Correct 2 ms 648 KB Output is correct
10 Correct 2 ms 648 KB Output is correct
11 Correct 1 ms 648 KB Output is correct
12 Correct 1 ms 648 KB Output is correct
13 Correct 1 ms 648 KB Output is correct
14 Correct 1 ms 648 KB Output is correct
15 Correct 1 ms 648 KB Output is correct
16 Correct 1 ms 648 KB Output is correct
17 Correct 1 ms 648 KB Output is correct
18 Correct 1 ms 648 KB Output is correct
19 Correct 2 ms 648 KB Output is correct
20 Correct 2 ms 648 KB Output is correct
21 Correct 1 ms 648 KB Output is correct
22 Correct 2 ms 648 KB Output is correct
23 Correct 2 ms 648 KB Output is correct
24 Correct 2 ms 648 KB Output is correct
25 Correct 2 ms 648 KB Output is correct
26 Correct 2 ms 648 KB Output is correct
27 Correct 2 ms 648 KB Output is correct
28 Correct 2 ms 648 KB Output is correct
29 Correct 3 ms 648 KB Output is correct
30 Correct 315 ms 20116 KB Output is correct
31 Correct 305 ms 20172 KB Output is correct
32 Correct 173 ms 20172 KB Output is correct
33 Correct 176 ms 20172 KB Output is correct
34 Correct 180 ms 20172 KB Output is correct
35 Correct 186 ms 20172 KB Output is correct
36 Correct 219 ms 20172 KB Output is correct
37 Correct 198 ms 20172 KB Output is correct
38 Correct 208 ms 20172 KB Output is correct
39 Correct 212 ms 20172 KB Output is correct
40 Correct 209 ms 20172 KB Output is correct
41 Correct 239 ms 20172 KB Output is correct
42 Correct 241 ms 20172 KB Output is correct
43 Correct 182 ms 20172 KB Output is correct
44 Correct 2 ms 20172 KB Output is correct
45 Correct 1 ms 20172 KB Output is correct
46 Correct 2 ms 20172 KB Output is correct
47 Correct 2 ms 20172 KB Output is correct
48 Correct 2 ms 20172 KB Output is correct
49 Correct 2 ms 20172 KB Output is correct
50 Correct 2 ms 20172 KB Output is correct
51 Correct 2 ms 20172 KB Output is correct
52 Correct 2 ms 20172 KB Output is correct
53 Correct 2 ms 20172 KB Output is correct
54 Correct 2 ms 20172 KB Output is correct
55 Correct 2 ms 20172 KB Output is correct
56 Correct 2 ms 20172 KB Output is correct
57 Correct 2 ms 20172 KB Output is correct
58 Correct 2 ms 20172 KB Output is correct
59 Correct 2 ms 20172 KB Output is correct
60 Correct 2 ms 20172 KB Output is correct
61 Correct 2 ms 20172 KB Output is correct
62 Correct 2 ms 20172 KB Output is correct
63 Correct 2 ms 20172 KB Output is correct
64 Correct 184 ms 20172 KB Output is correct
65 Correct 194 ms 20172 KB Output is correct
66 Correct 209 ms 20172 KB Output is correct
67 Correct 201 ms 20172 KB Output is correct
68 Correct 22 ms 20172 KB Output is correct
69 Correct 22 ms 20172 KB Output is correct
70 Correct 20 ms 20172 KB Output is correct
71 Correct 20 ms 20172 KB Output is correct
72 Correct 21 ms 20172 KB Output is correct
73 Correct 21 ms 20172 KB Output is correct
74 Correct 186 ms 20172 KB Output is correct
75 Correct 183 ms 20172 KB Output is correct
76 Correct 178 ms 20172 KB Output is correct
77 Correct 187 ms 20172 KB Output is correct
78 Correct 197 ms 20172 KB Output is correct
79 Correct 188 ms 20172 KB Output is correct
80 Correct 193 ms 20172 KB Output is correct
81 Correct 187 ms 21528 KB Output is correct
82 Correct 187 ms 21528 KB Output is correct
83 Correct 197 ms 21528 KB Output is correct
84 Correct 214 ms 21528 KB Output is correct
85 Correct 208 ms 21528 KB Output is correct
86 Correct 213 ms 21528 KB Output is correct
87 Correct 210 ms 21528 KB Output is correct
88 Correct 210 ms 21528 KB Output is correct
89 Correct 207 ms 21528 KB Output is correct
90 Correct 207 ms 21528 KB Output is correct
91 Correct 213 ms 21528 KB Output is correct
92 Correct 236 ms 21528 KB Output is correct