Submission #353223

# Submission time Handle Problem Language Result Execution time Memory
353223 2021-01-21T06:54:03 Z dolphingarlic Ancient Books (IOI17_books) C++14
70 / 100
2000 ms 114372 KB
#include "books.h"

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

int n, crit_l, crit_r, cmp_cnt;
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}];
    ll ans = LLONG_MAX;
    if (l != crit_l) ans = min(ans, compute(l - 1, r) + 2);
    if (r != crit_r) ans = min(ans, compute(l, r + 1) + 2);
    return dp[{l, r}] = ans;
}

ll minimum_walk(vector<int> p, int s) {
    n = p.size();
    fill(cmp, cmp + n, -1);
    cmp_cnt = 0, 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 0 ms 364 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 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 0 ms 364 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 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 492 KB Output is correct
20 Correct 1 ms 380 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 0 ms 364 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 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 492 KB Output is correct
20 Correct 1 ms 380 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 213 ms 18884 KB Output is correct
31 Correct 230 ms 18884 KB Output is correct
32 Correct 185 ms 26604 KB Output is correct
33 Correct 367 ms 114372 KB Output is correct
34 Correct 385 ms 114284 KB Output is correct
35 Correct 323 ms 87276 KB Output is correct
36 Correct 237 ms 39764 KB Output is correct
37 Correct 148 ms 20076 KB Output is correct
38 Correct 145 ms 19052 KB Output is correct
39 Correct 152 ms 18924 KB Output is correct
40 Correct 146 ms 18884 KB Output is correct
41 Correct 199 ms 18796 KB Output is correct
42 Correct 159 ms 18940 KB Output is correct
43 Correct 193 ms 24556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 58 ms 7124 KB Output is correct
2 Correct 2 ms 364 KB Output is correct
3 Correct 16 ms 2796 KB Output is correct
4 Correct 45 ms 6508 KB Output is correct
5 Correct 54 ms 6636 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 3 ms 876 KB Output is correct
12 Correct 4 ms 876 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 492 KB Output is correct
18 Correct 1 ms 492 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 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 492 KB Output is correct
20 Correct 1 ms 380 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 213 ms 18884 KB Output is correct
31 Correct 230 ms 18884 KB Output is correct
32 Correct 185 ms 26604 KB Output is correct
33 Correct 367 ms 114372 KB Output is correct
34 Correct 385 ms 114284 KB Output is correct
35 Correct 323 ms 87276 KB Output is correct
36 Correct 237 ms 39764 KB Output is correct
37 Correct 148 ms 20076 KB Output is correct
38 Correct 145 ms 19052 KB Output is correct
39 Correct 152 ms 18924 KB Output is correct
40 Correct 146 ms 18884 KB Output is correct
41 Correct 199 ms 18796 KB Output is correct
42 Correct 159 ms 18940 KB Output is correct
43 Correct 193 ms 24556 KB Output is correct
44 Correct 58 ms 7124 KB Output is correct
45 Correct 2 ms 364 KB Output is correct
46 Correct 16 ms 2796 KB Output is correct
47 Correct 45 ms 6508 KB Output is correct
48 Correct 54 ms 6636 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 3 ms 876 KB Output is correct
55 Correct 4 ms 876 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 492 KB Output is correct
61 Correct 1 ms 492 KB Output is correct
62 Correct 1 ms 364 KB Output is correct
63 Correct 1 ms 384 KB Output is correct
64 Execution timed out 2061 ms 25132 KB Time limit exceeded
65 Halted 0 ms 0 KB -