Submission #353336

# Submission time Handle Problem Language Result Execution time Memory
353336 2021-01-21T07:21:58 Z dolphingarlic Ancient Books (IOI17_books) C++14
100 / 100
445 ms 137708 KB
#include "books.h"

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

int crit_l, crit_r;
int cmp[1000000], cmp_l[1000000], cmp_r[1000000];
map<pair<int, int>, ll> dp;

void extend(int &l, int &r) {
    int ext_l = min(cmp_l[cmp[l]], cmp_l[cmp[r]]);
    int ext_r = max(cmp_r[cmp[l]], cmp_r[cmp[r]]);
    while (l != ext_l || r != ext_r) {
        if (l != ext_l) {
            l--;
            ext_l = min(ext_l, cmp_l[cmp[l]]);
            ext_r = max(ext_r, cmp_r[cmp[l]]);
        } else {
            r++;
            ext_l = min(ext_l, cmp_l[cmp[r]]);
            ext_r = max(ext_r, cmp_r[cmp[r]]);
        }
    }
}

ll compute(int l, int r) {
    extend(l, r);
    if (dp.count({l, r})) return dp[{l, r}];
    if (l == crit_l) return dp[{l, r}] = compute(l, r + 1) + 2;
    if (r == crit_r) return dp[{l, r}] = compute(l - 1, r) + 2;
    ll to_l = 2, to_r = 2;
    int nl, nr, tl, tr;
    nl = l - 1, nr = r, tl = l, tr = r + 1;
    extend(nl, nr), extend(tl, tr);
    while (nl != crit_l && nr < tr) {
        to_l += 2;
        nl--;
        extend(nl, nr);
    }
    if (nr < tr) return dp[{l, r}] = to_l + compute(nl, nr);
    nl = l, nr = r + 1, tl = l - 1, tr = r;
    extend(nl, nr), extend(tl, tr);
    while (nr != crit_r && nl > tl) {
        to_r += 2;
        nr++;
        extend(nl, nr);
    }
    return dp[{l, r}] = min(to_l, to_r) + compute(nl, nr);
}

ll minimum_walk(vector<int> p, int s) {
    int n = p.size(), cmp_cnt = 0;
    fill(cmp, cmp + n, -1);
    crit_l = s, crit_r = s;
    ll intra = 0;
    for (int i = 0; i < n; i++) {
        intra += abs(p[i] - i);
        if (cmp[i] == -1) {
            int curr = i;
            cmp_l[cmp_cnt] = cmp_r[cmp_cnt] = i;
            do {
                cmp[curr] = cmp_cnt;
                cmp_r[cmp_cnt] = max(cmp_r[cmp_cnt], curr);
                curr = p[curr];
            } while (curr != i);
            if (i != p[i]) {
                crit_l = min(crit_l, cmp_l[cmp_cnt]);
                crit_r = max(crit_r, cmp_r[cmp_cnt]);
            }
            cmp_cnt++;
        }
    }
    dp[{crit_l, crit_r}] = 0;
    return intra + compute(s, s);
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 492 KB Output is correct
23 Correct 1 ms 492 KB Output is correct
24 Correct 1 ms 492 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 512 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 492 KB Output is correct
23 Correct 1 ms 492 KB Output is correct
24 Correct 1 ms 492 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 512 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 215 ms 12012 KB Output is correct
31 Correct 226 ms 12012 KB Output is correct
32 Correct 185 ms 19948 KB Output is correct
33 Correct 380 ms 137708 KB Output is correct
34 Correct 445 ms 137584 KB Output is correct
35 Correct 299 ms 101868 KB Output is correct
36 Correct 176 ms 39276 KB Output is correct
37 Correct 138 ms 13676 KB Output is correct
38 Correct 194 ms 12396 KB Output is correct
39 Correct 202 ms 12268 KB Output is correct
40 Correct 146 ms 12136 KB Output is correct
41 Correct 157 ms 12100 KB Output is correct
42 Correct 144 ms 12100 KB Output is correct
43 Correct 138 ms 17900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 492 KB Output is correct
23 Correct 1 ms 492 KB Output is correct
24 Correct 1 ms 492 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 512 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 215 ms 12012 KB Output is correct
31 Correct 226 ms 12012 KB Output is correct
32 Correct 185 ms 19948 KB Output is correct
33 Correct 380 ms 137708 KB Output is correct
34 Correct 445 ms 137584 KB Output is correct
35 Correct 299 ms 101868 KB Output is correct
36 Correct 176 ms 39276 KB Output is correct
37 Correct 138 ms 13676 KB Output is correct
38 Correct 194 ms 12396 KB Output is correct
39 Correct 202 ms 12268 KB Output is correct
40 Correct 146 ms 12136 KB Output is correct
41 Correct 157 ms 12100 KB Output is correct
42 Correct 144 ms 12100 KB Output is correct
43 Correct 138 ms 17900 KB Output is correct
44 Correct 1 ms 364 KB Output is correct
45 Correct 1 ms 364 KB Output is correct
46 Correct 1 ms 364 KB Output is correct
47 Correct 1 ms 364 KB Output is correct
48 Correct 1 ms 364 KB Output is correct
49 Correct 1 ms 364 KB Output is correct
50 Correct 1 ms 364 KB Output is correct
51 Correct 1 ms 364 KB Output is correct
52 Correct 1 ms 364 KB Output is correct
53 Correct 1 ms 364 KB Output is correct
54 Correct 1 ms 364 KB Output is correct
55 Correct 1 ms 364 KB Output is correct
56 Correct 1 ms 364 KB Output is correct
57 Correct 1 ms 364 KB Output is correct
58 Correct 1 ms 364 KB Output is correct
59 Correct 1 ms 384 KB Output is correct
60 Correct 1 ms 364 KB Output is correct
61 Correct 1 ms 364 KB Output is correct
62 Correct 1 ms 364 KB Output is correct
63 Correct 1 ms 492 KB Output is correct
64 Correct 138 ms 17772 KB Output is correct
65 Correct 157 ms 25340 KB Output is correct
66 Correct 143 ms 19164 KB Output is correct
67 Correct 132 ms 18924 KB Output is correct
68 Correct 14 ms 2284 KB Output is correct
69 Correct 14 ms 2156 KB Output is correct
70 Correct 15 ms 2412 KB Output is correct
71 Correct 17 ms 2668 KB Output is correct
72 Correct 14 ms 2796 KB Output is correct
73 Correct 14 ms 2156 KB Output is correct
74 Correct 226 ms 72260 KB Output is correct
75 Correct 248 ms 84332 KB Output is correct
76 Correct 149 ms 24684 KB Output is correct
77 Correct 152 ms 24556 KB Output is correct
78 Correct 167 ms 23748 KB Output is correct
79 Correct 154 ms 23824 KB Output is correct
80 Correct 137 ms 26692 KB Output is correct
81 Correct 257 ms 85312 KB Output is correct
82 Correct 262 ms 85332 KB Output is correct
83 Correct 155 ms 27372 KB Output is correct
84 Correct 163 ms 25196 KB Output is correct
85 Correct 138 ms 21228 KB Output is correct
86 Correct 141 ms 19692 KB Output is correct
87 Correct 192 ms 54380 KB Output is correct
88 Correct 189 ms 41600 KB Output is correct
89 Correct 174 ms 30060 KB Output is correct
90 Correct 136 ms 19196 KB Output is correct
91 Correct 150 ms 18888 KB Output is correct
92 Correct 143 ms 18884 KB Output is correct