Submission #353350

# Submission time Handle Problem Language Result Execution time Memory
353350 2021-01-21T07:23:23 Z dolphingarlic Ancient Books (IOI17_books) C++14
100 / 100
428 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;
    int 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);

    ll to_r = 2;
    nl = l, nr = r + 1, tl = l - 1, tr = r;
    extend(nl, nr), extend(tl, tr);
    while (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 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 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 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 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 364 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 364 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 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 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 364 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 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 225 ms 12140 KB Output is correct
31 Correct 225 ms 12100 KB Output is correct
32 Correct 168 ms 19852 KB Output is correct
33 Correct 428 ms 137708 KB Output is correct
34 Correct 379 ms 137580 KB Output is correct
35 Correct 334 ms 102236 KB Output is correct
36 Correct 239 ms 39148 KB Output is correct
37 Correct 185 ms 13676 KB Output is correct
38 Correct 199 ms 12396 KB Output is correct
39 Correct 152 ms 12268 KB Output is correct
40 Correct 188 ms 12100 KB Output is correct
41 Correct 169 ms 12236 KB Output is correct
42 Correct 169 ms 12012 KB Output is correct
43 Correct 146 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 512 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
# 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 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 364 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 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 225 ms 12140 KB Output is correct
31 Correct 225 ms 12100 KB Output is correct
32 Correct 168 ms 19852 KB Output is correct
33 Correct 428 ms 137708 KB Output is correct
34 Correct 379 ms 137580 KB Output is correct
35 Correct 334 ms 102236 KB Output is correct
36 Correct 239 ms 39148 KB Output is correct
37 Correct 185 ms 13676 KB Output is correct
38 Correct 199 ms 12396 KB Output is correct
39 Correct 152 ms 12268 KB Output is correct
40 Correct 188 ms 12100 KB Output is correct
41 Correct 169 ms 12236 KB Output is correct
42 Correct 169 ms 12012 KB Output is correct
43 Correct 146 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 512 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 364 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 364 KB Output is correct
64 Correct 144 ms 17772 KB Output is correct
65 Correct 169 ms 18412 KB Output is correct
66 Correct 143 ms 12268 KB Output is correct
67 Correct 147 ms 12268 KB Output is correct
68 Correct 15 ms 1772 KB Output is correct
69 Correct 14 ms 1644 KB Output is correct
70 Correct 14 ms 1772 KB Output is correct
71 Correct 18 ms 2028 KB Output is correct
72 Correct 14 ms 2156 KB Output is correct
73 Correct 15 ms 1516 KB Output is correct
74 Correct 230 ms 65516 KB Output is correct
75 Correct 274 ms 77548 KB Output is correct
76 Correct 148 ms 17772 KB Output is correct
77 Correct 151 ms 17900 KB Output is correct
78 Correct 150 ms 17076 KB Output is correct
79 Correct 145 ms 17004 KB Output is correct
80 Correct 179 ms 19968 KB Output is correct
81 Correct 272 ms 78700 KB Output is correct
82 Correct 331 ms 78572 KB Output is correct
83 Correct 188 ms 20676 KB Output is correct
84 Correct 185 ms 18540 KB Output is correct
85 Correct 181 ms 14572 KB Output is correct
86 Correct 161 ms 12908 KB Output is correct
87 Correct 247 ms 47684 KB Output is correct
88 Correct 186 ms 34668 KB Output is correct
89 Correct 231 ms 23276 KB Output is correct
90 Correct 141 ms 12396 KB Output is correct
91 Correct 141 ms 12228 KB Output is correct
92 Correct 171 ms 12100 KB Output is correct