Submission #239133

# Submission time Handle Problem Language Result Execution time Memory
239133 2020-06-14T14:34:28 Z lyc Ancient Books (IOI17_books) C++14
100 / 100
273 ms 26616 KB
#include "books.h"
#include <bits/stdc++.h>
using namespace std;

#define TRACE(x) cout << #x << " :: " << x << endl;
#define _ << " " <<
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()

const int mxN = 1e6+5;
int N, S, E;
int C[mxN], L[mxN], R[mxN], cc;

void ext(int& l, int& r) {
    int x = min(L[C[l]],L[C[r]]), y = max(R[C[l]],R[C[r]]);
    while (true) {
        if (x < l) {
            --l;
            x = min(x,L[C[l]]);
            y = max(y,R[C[l]]);
        } else if (r < y) {
            ++r;
            x = min(x,L[C[r]]);
            y = max(y,R[C[r]]);
        } else break;
    }
}

long long minimum_walk(std::vector<int> p, int s) {
    N = SZ(p);
    long long lb = 0;
    cc = 0;
    S = N-1, E = 0;
    FOR(i,0,N-1){
        lb += abs(p[i]-i);
        if (C[i]) continue;
        C[i] = ++cc;
        L[cc] = R[cc] = i;
        for (int u = p[i]; u != i; u = p[u]) {
            C[u] = cc;
            L[cc] = min(L[cc],u);
            R[cc] = max(R[cc],u);
        }
        if (L[cc] != R[cc]) S = min(S,L[cc]), E = max(E,R[cc]);
    }
    long long add = 0;
    int l = s, r = s;
    ext(l,r);
    while (S < l || r < E) {
        bool op = 0;
        int cl = 0, cr = 0, nl = -1, nr = -1;
        int x = l, y = r;
        while (S < x) { // calc cl
            cl += 2;
            ext(--x,y);
            if (y > r) { op = 1; break; }
        }
        nl = x;
        x = l, y = r;
        while (y < E) { // calc cr
            cr += 2;
            ext(x,++y);
            if (x < l) { op = 1; break; }
        }
        nr = y;
        if (op) {
            add += min(cl,cr);
        } else { // neither covers 
            add += cl + cr;
        }
        l = nl, r = nr;
    }
    //TRACE(lb _ add);
    return lb + add;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 256 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 4 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 4 ms 384 KB Output is correct
16 Correct 4 ms 256 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 4 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 256 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 4 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 4 ms 384 KB Output is correct
16 Correct 4 ms 256 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 4 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 5 ms 512 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 5 ms 384 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 5 ms 384 KB Output is correct
29 Correct 5 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 256 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 4 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 4 ms 384 KB Output is correct
16 Correct 4 ms 256 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 4 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 5 ms 512 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 5 ms 384 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 5 ms 384 KB Output is correct
29 Correct 5 ms 512 KB Output is correct
30 Correct 212 ms 18808 KB Output is correct
31 Correct 273 ms 18812 KB Output is correct
32 Correct 143 ms 26616 KB Output is correct
33 Correct 158 ms 24252 KB Output is correct
34 Correct 165 ms 24184 KB Output is correct
35 Correct 159 ms 23032 KB Output is correct
36 Correct 151 ms 20856 KB Output is correct
37 Correct 153 ms 19064 KB Output is correct
38 Correct 153 ms 18808 KB Output is correct
39 Correct 159 ms 18808 KB Output is correct
40 Correct 163 ms 18936 KB Output is correct
41 Correct 188 ms 18808 KB Output is correct
42 Correct 179 ms 18808 KB Output is correct
43 Correct 155 ms 24568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 256 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 4 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 4 ms 384 KB Output is correct
16 Correct 4 ms 256 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 4 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 5 ms 512 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 5 ms 384 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 5 ms 384 KB Output is correct
29 Correct 5 ms 512 KB Output is correct
30 Correct 212 ms 18808 KB Output is correct
31 Correct 273 ms 18812 KB Output is correct
32 Correct 143 ms 26616 KB Output is correct
33 Correct 158 ms 24252 KB Output is correct
34 Correct 165 ms 24184 KB Output is correct
35 Correct 159 ms 23032 KB Output is correct
36 Correct 151 ms 20856 KB Output is correct
37 Correct 153 ms 19064 KB Output is correct
38 Correct 153 ms 18808 KB Output is correct
39 Correct 159 ms 18808 KB Output is correct
40 Correct 163 ms 18936 KB Output is correct
41 Correct 188 ms 18808 KB Output is correct
42 Correct 179 ms 18808 KB Output is correct
43 Correct 155 ms 24568 KB Output is correct
44 Correct 5 ms 384 KB Output is correct
45 Correct 5 ms 384 KB Output is correct
46 Correct 5 ms 384 KB Output is correct
47 Correct 5 ms 384 KB Output is correct
48 Correct 5 ms 384 KB Output is correct
49 Correct 5 ms 384 KB Output is correct
50 Correct 5 ms 384 KB Output is correct
51 Correct 5 ms 384 KB Output is correct
52 Correct 5 ms 384 KB Output is correct
53 Correct 5 ms 384 KB Output is correct
54 Correct 5 ms 384 KB Output is correct
55 Correct 5 ms 384 KB Output is correct
56 Correct 5 ms 384 KB Output is correct
57 Correct 5 ms 384 KB Output is correct
58 Correct 5 ms 384 KB Output is correct
59 Correct 5 ms 384 KB Output is correct
60 Correct 5 ms 384 KB Output is correct
61 Correct 5 ms 384 KB Output is correct
62 Correct 5 ms 384 KB Output is correct
63 Correct 5 ms 384 KB Output is correct
64 Correct 157 ms 24568 KB Output is correct
65 Correct 150 ms 25160 KB Output is correct
66 Correct 152 ms 19192 KB Output is correct
67 Correct 162 ms 18936 KB Output is correct
68 Correct 19 ms 2304 KB Output is correct
69 Correct 19 ms 2176 KB Output is correct
70 Correct 19 ms 2432 KB Output is correct
71 Correct 19 ms 2688 KB Output is correct
72 Correct 19 ms 2816 KB Output is correct
73 Correct 20 ms 2048 KB Output is correct
74 Correct 154 ms 24184 KB Output is correct
75 Correct 159 ms 24312 KB Output is correct
76 Correct 163 ms 24568 KB Output is correct
77 Correct 159 ms 24632 KB Output is correct
78 Correct 159 ms 23804 KB Output is correct
79 Correct 153 ms 23800 KB Output is correct
80 Correct 145 ms 26616 KB Output is correct
81 Correct 151 ms 22720 KB Output is correct
82 Correct 152 ms 22776 KB Output is correct
83 Correct 153 ms 21752 KB Output is correct
84 Correct 162 ms 21240 KB Output is correct
85 Correct 153 ms 19976 KB Output is correct
86 Correct 150 ms 19192 KB Output is correct
87 Correct 164 ms 25464 KB Output is correct
88 Correct 162 ms 24308 KB Output is correct
89 Correct 159 ms 22648 KB Output is correct
90 Correct 153 ms 19064 KB Output is correct
91 Correct 157 ms 18888 KB Output is correct
92 Correct 161 ms 18808 KB Output is correct