Submission #1074741

# Submission time Handle Problem Language Result Execution time Memory
1074741 2024-08-25T13:30:58 Z ArthuroWich Ancient Books (IOI17_books) C++17
70 / 100
2000 ms 52924 KB
#include "books.h"
#include<bits/stdc++.h>
using namespace std;
int n, minl, maxr;
vector<int> pref, f1, f2;
vector<pair<int, int>> cyc;
map<pair<int, int>, int> dp;
int calc(int l, int r) {
    int lv, rv;
    lv = min(l, min(cyc[l].first, cyc[r].first));
    rv = max(r, max(cyc[l].second, cyc[r].second));
    while(lv < l || r < rv) {
        if (lv < l) {
            l--;
            lv = min(lv, cyc[l].first);
            rv = max(rv, cyc[l].second);
        } else {
            r++;
            lv = min(lv, cyc[r].first);
            rv = max(rv, cyc[r].second);
        }
    }
    if (dp.find({l, r}) != dp.end()) {
        return dp[{l, r}];
    }

    if (l-1 < minl && r+1 > maxr) {
        dp[{l, r}] = 0;
        return 0;
    } 
    if (l-1 < minl) {
        dp[{l, r}] = 2+calc(l, r+1);
        return dp[{l, r}];
    } else if (r+1 > maxr) {
        dp[{l, r}] = 2+calc(l-1, r);
        return dp[{l, r}];
    } else {
        dp[{l, r}] = 2+min(calc(l-1, r), calc(l, r+1));
        return dp[{l, r}];
    }
}
long long minimum_walk(vector<int> p, int s) {
    long long int ans = 0;
    n = p.size();
    pref.resize(n+1, 0);
    f1.resize(n, 0);
    f2.resize(n, 0);
    cyc.resize(n);
    minl = INT_MAX;
    maxr = 0;
    for (int i = 0; i < n; i++) {
        ans += abs(i-p[i]);
    }
    vector<pair<int, int>> cycles;
    vector<int> vis(n, 0);
    for (int i = 0; i < n; i++) {
        if (p[i] == i) {
            cyc[i] = {i, i};
            continue;
        }
        if (vis[i]) {
            continue;
        }
        int a = i, l = i, r = i;
        vis[i] = 1;
        a = p[i];
        vis[a] = 1;
        l = min(l, a);
        r = max(r, a);
        while(a != i) {
            a = p[a];
            vis[a] = 1;
            l = min(l, a);
            r = max(r, a);
        }
        a = p[a];
        cyc[a] = {l, r};
        while(a != i) {
            a = p[a];
            cyc[a] = {l, r};
        }
        minl = min(minl, l);
        maxr = max(maxr, r);
        cycles.push_back({min(l, r), max(l, r)});
    }
    for (auto [i, j] : cycles) {
        pref[i] += 1;
        pref[j] -= 1; 
    }
    for (int i = 1; i < n; i++) {
        pref[i] += pref[i-1];
    }
    if (s == 0) {
        for (int i = n-1; i >= 0; i--) {
            if (pref[i] > 0) {
                f2[i] = 1;
            }
            if (i < n-1) {
                f2[i] |= f2[i+1];
            }
        }
        for (int i = n-1; i >= 0; i--) {
            if (f2[i] && pref[i] == 0) {
                ans += 2;
            }
        }
    } else {
        ans += calc(s, s);
    }
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 600 KB Output is correct
26 Correct 1 ms 600 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 600 KB Output is correct
26 Correct 1 ms 600 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 133 ms 38228 KB Output is correct
31 Correct 130 ms 38224 KB Output is correct
32 Correct 98 ms 38240 KB Output is correct
33 Correct 92 ms 40544 KB Output is correct
34 Correct 96 ms 40644 KB Output is correct
35 Correct 94 ms 40548 KB Output is correct
36 Correct 134 ms 40652 KB Output is correct
37 Correct 83 ms 38764 KB Output is correct
38 Correct 87 ms 38480 KB Output is correct
39 Correct 88 ms 38388 KB Output is correct
40 Correct 97 ms 38376 KB Output is correct
41 Correct 115 ms 38224 KB Output is correct
42 Correct 113 ms 38228 KB Output is correct
43 Correct 123 ms 40644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 7020 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 11 ms 2864 KB Output is correct
4 Correct 31 ms 6480 KB Output is correct
5 Correct 41 ms 6780 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 3 ms 860 KB Output is correct
12 Correct 2 ms 860 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 440 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 432 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 600 KB Output is correct
26 Correct 1 ms 600 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 133 ms 38228 KB Output is correct
31 Correct 130 ms 38224 KB Output is correct
32 Correct 98 ms 38240 KB Output is correct
33 Correct 92 ms 40544 KB Output is correct
34 Correct 96 ms 40644 KB Output is correct
35 Correct 94 ms 40548 KB Output is correct
36 Correct 134 ms 40652 KB Output is correct
37 Correct 83 ms 38764 KB Output is correct
38 Correct 87 ms 38480 KB Output is correct
39 Correct 88 ms 38388 KB Output is correct
40 Correct 97 ms 38376 KB Output is correct
41 Correct 115 ms 38224 KB Output is correct
42 Correct 113 ms 38228 KB Output is correct
43 Correct 123 ms 40644 KB Output is correct
44 Correct 54 ms 7020 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 11 ms 2864 KB Output is correct
47 Correct 31 ms 6480 KB Output is correct
48 Correct 41 ms 6780 KB Output is correct
49 Correct 1 ms 348 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 1 ms 348 KB Output is correct
52 Correct 1 ms 344 KB Output is correct
53 Correct 1 ms 348 KB Output is correct
54 Correct 3 ms 860 KB Output is correct
55 Correct 2 ms 860 KB Output is correct
56 Correct 1 ms 344 KB Output is correct
57 Correct 1 ms 348 KB Output is correct
58 Correct 0 ms 440 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 1 ms 432 KB Output is correct
61 Correct 1 ms 344 KB Output is correct
62 Correct 1 ms 348 KB Output is correct
63 Correct 1 ms 344 KB Output is correct
64 Execution timed out 2041 ms 52924 KB Time limit exceeded
65 Halted 0 ms 0 KB -