Submission #799549

# Submission time Handle Problem Language Result Execution time Memory
799549 2023-07-31T15:54:17 Z PixelCat Ancient Books (IOI17_books) C++14
100 / 100
132 ms 46284 KB
#include "books.h"
 
#ifdef NYAOWO
#include "grader.cpp"
#endif
 
#include <bits/stdc++.h>
#define For(i, a, b) for(int i = a; i <= b; i++)
#define Forr(i, a, b) for(int i = a; i >= b; i--)
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sz(x) ((int)x.size())
#define eb emplace_back
#define int LL
using namespace std;
using i32 = int32_t;
using LL = long long;
using pii = pair<int, int>;
 
const int MAXN = 1'000'000;
const int INF = 1e18;
 
LL minimum_walk(vector<i32> p, i32 s) {
    assert(s >= 0);
    int n = sz(p);
 
    int ans = 0;
    vector<int> cid(n, -1);
    vector<pii> range;
    For(i, 0, n - 1) if(p[i] != i && cid[i] == -1) {
        int cur = i;
        int l = n + 1;
        int r = -1;
        do {
            l = min(l, cur);
            r = max(r, cur);
            ans += abs(p[cur] - cur);
            cur = p[cur];
            cid[cur] = sz(range);
        } while(cur != i);
        range.eb(l, r);
    }
    if(sz(range) == 0) return 0;
 
    int L = 0, R = n - 1;
    while(cid[L] == -1) L++;
    while(cid[R] == -1) R--;
    L = min(L, (int)s);
    R = max(R, (int)s);
    cerr << L << " " << R << "\n";
 
    vector<int> nxtl(n), nxtr(n);
    nxtl[0] = -INF;
    For(i, 1, n - 1) {
        if(cid[i - 1] != -1) nxtl[i] = i - 1;
        else nxtl[i] = nxtl[i - 1];
    }
    nxtr[n - 1] = INF;
    Forr(i, n - 2, 0) {
        if(cid[i + 1] != -1) nxtr[i] = i + 1;
        else nxtr[i] = nxtr[i + 1];
    }

    // For(i, 0, n - 1) cerr << nxtl[i] << " \n"[i == n - 1];
    // For(i, 0, n - 1) cerr << nxtr[i] << " \n"[i == n - 1];
    // return 0;

    auto walk = [&](int l, int r, pii target) {
        int tl, tr;
        tie(tl, tr) = target;
        tl = min(tl, l);
        tr = max(tr, r);
        // cerr << l << " " << r << " " << tl << " " << tr << "\n";
        while(l > tl || r < tr) {
            if(l > tl) {
                l--;
                if(cid[l] != -1) {
                    tl = min(tl, range[cid[l]].F);
                    tr = max(tr, range[cid[l]].S);
                }
            } else {
                r++;
                if(cid[r] != -1) {
                    tl = min(tl, range[cid[r]].F);
                    tr = max(tr, range[cid[r]].S);
                }
            }
        }
        return pii(l, r);
    };
 
    int l = s, r = s;
    if(cid[s] != -1) {
        tie(l, r) = walk(l, r, range[cid[s]]);
    }
    while(l > L || r < R) {

        int cl = 0;
        int l2, r2;
        l2 = l; r2 = r;
        if(l2 == L) cl = INF;
        while(r2 == r && l2 != L) {
            cl += (l2 - nxtl[l2]) * 2;
            l2 = nxtl[l2];
            tie(l2, r2) = walk(l2, r2, range[cid[l2]]);
        }

        int cr = 0;
        int l3, r3;
        l3 = l; r3 = r;
        if(r3 == R) cr = INF;
        while(l3 == l && r3 != R) {
            cr += (nxtr[r3] - r3) * 2;
            r3 = nxtr[r3];
            tie(l3, r3) = walk(l3, r3, range[cid[r3]]);
        }

        if(cl < cr) {
            ans += cl;
            tie(l, r) = walk(l, r, pii(l2, r2));
        } else {
            ans += cr;
            tie(l, r) = walk(l, r, pii(l3, r3));
        }
    }
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 125 ms 31596 KB Output is correct
31 Correct 105 ms 31572 KB Output is correct
32 Correct 74 ms 15848 KB Output is correct
33 Correct 101 ms 34744 KB Output is correct
34 Correct 101 ms 34660 KB Output is correct
35 Correct 132 ms 35036 KB Output is correct
36 Correct 95 ms 34148 KB Output is correct
37 Correct 89 ms 32068 KB Output is correct
38 Correct 89 ms 31836 KB Output is correct
39 Correct 90 ms 31724 KB Output is correct
40 Correct 96 ms 31612 KB Output is correct
41 Correct 121 ms 31600 KB Output is correct
42 Correct 99 ms 31592 KB Output is correct
43 Correct 104 ms 35552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 300 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 296 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 125 ms 31596 KB Output is correct
31 Correct 105 ms 31572 KB Output is correct
32 Correct 74 ms 15848 KB Output is correct
33 Correct 101 ms 34744 KB Output is correct
34 Correct 101 ms 34660 KB Output is correct
35 Correct 132 ms 35036 KB Output is correct
36 Correct 95 ms 34148 KB Output is correct
37 Correct 89 ms 32068 KB Output is correct
38 Correct 89 ms 31836 KB Output is correct
39 Correct 90 ms 31724 KB Output is correct
40 Correct 96 ms 31612 KB Output is correct
41 Correct 121 ms 31600 KB Output is correct
42 Correct 99 ms 31592 KB Output is correct
43 Correct 104 ms 35552 KB Output is correct
44 Correct 0 ms 340 KB Output is correct
45 Correct 0 ms 212 KB Output is correct
46 Correct 0 ms 340 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 1 ms 340 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 1 ms 340 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 1 ms 212 KB Output is correct
54 Correct 1 ms 340 KB Output is correct
55 Correct 1 ms 340 KB Output is correct
56 Correct 1 ms 300 KB Output is correct
57 Correct 1 ms 340 KB Output is correct
58 Correct 1 ms 296 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Correct 1 ms 340 KB Output is correct
62 Correct 1 ms 340 KB Output is correct
63 Correct 1 ms 296 KB Output is correct
64 Correct 103 ms 40140 KB Output is correct
65 Correct 103 ms 39888 KB Output is correct
66 Correct 91 ms 38736 KB Output is correct
67 Correct 93 ms 38612 KB Output is correct
68 Correct 10 ms 4316 KB Output is correct
69 Correct 13 ms 4180 KB Output is correct
70 Correct 10 ms 4168 KB Output is correct
71 Correct 11 ms 4276 KB Output is correct
72 Correct 10 ms 4304 KB Output is correct
73 Correct 9 ms 4024 KB Output is correct
74 Correct 105 ms 41488 KB Output is correct
75 Correct 108 ms 41416 KB Output is correct
76 Correct 114 ms 42308 KB Output is correct
77 Correct 115 ms 42348 KB Output is correct
78 Correct 114 ms 44232 KB Output is correct
79 Correct 110 ms 44244 KB Output is correct
80 Correct 76 ms 22628 KB Output is correct
81 Correct 97 ms 46260 KB Output is correct
82 Correct 96 ms 46284 KB Output is correct
83 Correct 130 ms 42200 KB Output is correct
84 Correct 102 ms 41668 KB Output is correct
85 Correct 96 ms 40124 KB Output is correct
86 Correct 102 ms 39132 KB Output is correct
87 Correct 106 ms 40776 KB Output is correct
88 Correct 109 ms 43068 KB Output is correct
89 Correct 107 ms 42708 KB Output is correct
90 Correct 94 ms 38640 KB Output is correct
91 Correct 103 ms 38452 KB Output is correct
92 Correct 94 ms 38360 KB Output is correct