Submission #54991

# Submission time Handle Problem Language Result Execution time Memory
54991 2018-07-05T17:05:25 Z fallingstar Ancient Books (IOI17_books) C++14
100 / 100
435 ms 183180 KB
#include "books.h"
#include <algorithm>

#define long long long

using namespace std;

const int N = 1e6 + 2;

vector<int> p;
int gr[N], boundL[N], boundR[N];

void Dfs(int u) 
{ 
    if (gr[p[u]] == -1) 
    {
        gr[p[u]] = gr[u];
        Dfs(p[u]);         
    }
    boundL[gr[u]] = min(boundL[gr[u]], u);
    boundR[gr[u]] = max(boundR[gr[u]], u);
}

long minimum_walk(std::vector<int> p_, int s) {
    p = p_;
	int n = p.size(), L = 0, R = n - 1;
    while (R > s && p[R] == R) --R;
    while (L < s && p[L] == L) ++L;
    
    fill(gr + L, gr + R + 1, -1);
    for (int i = 0; i < n; ++i)
        boundL[i] = n, boundR[i] = 0;
    
    long ans = 0;
    for (int i = L; i <= R; ++i)
    {
        if (gr[i] == -1) gr[i] = i, Dfs(i);
        ans += abs(i - p[i]);
    }

    int curL = s, curR = s;
    while (curL != L || curR != R)
    {
        int newL, newR;
        if (curL > boundL[gr[s]] || curR < boundR[gr[s]])     // first time
        {
            newL = boundL[gr[s]], newR = boundR[gr[s]];
        }
        else 
        {
            ans += 2;
            newL = curL - (curL > L), newR = curR + (curR < R);            
        }
        while (curL != newL || curR != newR)
        {
            if (curL > newL)
            {
                --curL;
                newL = min(newL, boundL[gr[curL]]);
                newR = max(newR, boundR[gr[curL]]);
            }
            else 
            {
                ++curR;
                newL = min(newL, boundL[gr[curR]]);
                newR = max(newR, boundR[gr[curR]]);
            }
        }
    }
    int valL = 0, valR = 0;
    for (int i = L, mx = 0; i < s; ++i)
    {
        mx = max(mx, p[i]);
        if (mx == i) valL += 2;
    }
    for (int i = R, mn = n; i > s; --i)
    {
        mn = min(mn, p[i]);
        if (mn == i) valR += 2;
    }
    return ans + min(valL, valR);
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 2 ms 436 KB Output is correct
4 Correct 2 ms 488 KB Output is correct
5 Correct 2 ms 488 KB Output is correct
6 Correct 2 ms 488 KB Output is correct
7 Correct 3 ms 536 KB Output is correct
8 Correct 3 ms 536 KB Output is correct
9 Correct 3 ms 536 KB Output is correct
10 Correct 2 ms 536 KB Output is correct
11 Correct 2 ms 572 KB Output is correct
12 Correct 3 ms 572 KB Output is correct
13 Correct 2 ms 572 KB Output is correct
14 Correct 2 ms 580 KB Output is correct
15 Correct 2 ms 588 KB Output is correct
16 Correct 2 ms 588 KB Output is correct
17 Correct 2 ms 588 KB Output is correct
18 Correct 2 ms 620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 2 ms 436 KB Output is correct
4 Correct 2 ms 488 KB Output is correct
5 Correct 2 ms 488 KB Output is correct
6 Correct 2 ms 488 KB Output is correct
7 Correct 3 ms 536 KB Output is correct
8 Correct 3 ms 536 KB Output is correct
9 Correct 3 ms 536 KB Output is correct
10 Correct 2 ms 536 KB Output is correct
11 Correct 2 ms 572 KB Output is correct
12 Correct 3 ms 572 KB Output is correct
13 Correct 2 ms 572 KB Output is correct
14 Correct 2 ms 580 KB Output is correct
15 Correct 2 ms 588 KB Output is correct
16 Correct 2 ms 588 KB Output is correct
17 Correct 2 ms 588 KB Output is correct
18 Correct 2 ms 620 KB Output is correct
19 Correct 2 ms 620 KB Output is correct
20 Correct 2 ms 620 KB Output is correct
21 Correct 2 ms 620 KB Output is correct
22 Correct 2 ms 732 KB Output is correct
23 Correct 2 ms 732 KB Output is correct
24 Correct 2 ms 732 KB Output is correct
25 Correct 3 ms 732 KB Output is correct
26 Correct 3 ms 732 KB Output is correct
27 Correct 2 ms 732 KB Output is correct
28 Correct 2 ms 732 KB Output is correct
29 Correct 2 ms 732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 2 ms 436 KB Output is correct
4 Correct 2 ms 488 KB Output is correct
5 Correct 2 ms 488 KB Output is correct
6 Correct 2 ms 488 KB Output is correct
7 Correct 3 ms 536 KB Output is correct
8 Correct 3 ms 536 KB Output is correct
9 Correct 3 ms 536 KB Output is correct
10 Correct 2 ms 536 KB Output is correct
11 Correct 2 ms 572 KB Output is correct
12 Correct 3 ms 572 KB Output is correct
13 Correct 2 ms 572 KB Output is correct
14 Correct 2 ms 580 KB Output is correct
15 Correct 2 ms 588 KB Output is correct
16 Correct 2 ms 588 KB Output is correct
17 Correct 2 ms 588 KB Output is correct
18 Correct 2 ms 620 KB Output is correct
19 Correct 2 ms 620 KB Output is correct
20 Correct 2 ms 620 KB Output is correct
21 Correct 2 ms 620 KB Output is correct
22 Correct 2 ms 732 KB Output is correct
23 Correct 2 ms 732 KB Output is correct
24 Correct 2 ms 732 KB Output is correct
25 Correct 3 ms 732 KB Output is correct
26 Correct 3 ms 732 KB Output is correct
27 Correct 2 ms 732 KB Output is correct
28 Correct 2 ms 732 KB Output is correct
29 Correct 2 ms 732 KB Output is correct
30 Correct 407 ms 48508 KB Output is correct
31 Correct 435 ms 48508 KB Output is correct
32 Correct 179 ms 48508 KB Output is correct
33 Correct 173 ms 48508 KB Output is correct
34 Correct 209 ms 48508 KB Output is correct
35 Correct 176 ms 48508 KB Output is correct
36 Correct 184 ms 48508 KB Output is correct
37 Correct 212 ms 48508 KB Output is correct
38 Correct 209 ms 48508 KB Output is correct
39 Correct 242 ms 48508 KB Output is correct
40 Correct 230 ms 48508 KB Output is correct
41 Correct 298 ms 48508 KB Output is correct
42 Correct 279 ms 48508 KB Output is correct
43 Correct 251 ms 48508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 48508 KB Output is correct
2 Correct 2 ms 48508 KB Output is correct
3 Correct 2 ms 48508 KB Output is correct
4 Correct 2 ms 48508 KB Output is correct
5 Correct 3 ms 48508 KB Output is correct
6 Correct 3 ms 48508 KB Output is correct
7 Correct 3 ms 48508 KB Output is correct
8 Correct 2 ms 48508 KB Output is correct
9 Correct 2 ms 48508 KB Output is correct
10 Correct 3 ms 48508 KB Output is correct
11 Correct 2 ms 48508 KB Output is correct
12 Correct 2 ms 48508 KB Output is correct
13 Correct 2 ms 48508 KB Output is correct
14 Correct 2 ms 48508 KB Output is correct
15 Correct 2 ms 48508 KB Output is correct
16 Correct 2 ms 48508 KB Output is correct
17 Correct 2 ms 48508 KB Output is correct
18 Correct 2 ms 48508 KB Output is correct
19 Correct 2 ms 48508 KB Output is correct
20 Correct 3 ms 48508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 2 ms 436 KB Output is correct
4 Correct 2 ms 488 KB Output is correct
5 Correct 2 ms 488 KB Output is correct
6 Correct 2 ms 488 KB Output is correct
7 Correct 3 ms 536 KB Output is correct
8 Correct 3 ms 536 KB Output is correct
9 Correct 3 ms 536 KB Output is correct
10 Correct 2 ms 536 KB Output is correct
11 Correct 2 ms 572 KB Output is correct
12 Correct 3 ms 572 KB Output is correct
13 Correct 2 ms 572 KB Output is correct
14 Correct 2 ms 580 KB Output is correct
15 Correct 2 ms 588 KB Output is correct
16 Correct 2 ms 588 KB Output is correct
17 Correct 2 ms 588 KB Output is correct
18 Correct 2 ms 620 KB Output is correct
19 Correct 2 ms 620 KB Output is correct
20 Correct 2 ms 620 KB Output is correct
21 Correct 2 ms 620 KB Output is correct
22 Correct 2 ms 732 KB Output is correct
23 Correct 2 ms 732 KB Output is correct
24 Correct 2 ms 732 KB Output is correct
25 Correct 3 ms 732 KB Output is correct
26 Correct 3 ms 732 KB Output is correct
27 Correct 2 ms 732 KB Output is correct
28 Correct 2 ms 732 KB Output is correct
29 Correct 2 ms 732 KB Output is correct
30 Correct 407 ms 48508 KB Output is correct
31 Correct 435 ms 48508 KB Output is correct
32 Correct 179 ms 48508 KB Output is correct
33 Correct 173 ms 48508 KB Output is correct
34 Correct 209 ms 48508 KB Output is correct
35 Correct 176 ms 48508 KB Output is correct
36 Correct 184 ms 48508 KB Output is correct
37 Correct 212 ms 48508 KB Output is correct
38 Correct 209 ms 48508 KB Output is correct
39 Correct 242 ms 48508 KB Output is correct
40 Correct 230 ms 48508 KB Output is correct
41 Correct 298 ms 48508 KB Output is correct
42 Correct 279 ms 48508 KB Output is correct
43 Correct 251 ms 48508 KB Output is correct
44 Correct 3 ms 48508 KB Output is correct
45 Correct 2 ms 48508 KB Output is correct
46 Correct 2 ms 48508 KB Output is correct
47 Correct 2 ms 48508 KB Output is correct
48 Correct 3 ms 48508 KB Output is correct
49 Correct 3 ms 48508 KB Output is correct
50 Correct 3 ms 48508 KB Output is correct
51 Correct 2 ms 48508 KB Output is correct
52 Correct 2 ms 48508 KB Output is correct
53 Correct 3 ms 48508 KB Output is correct
54 Correct 2 ms 48508 KB Output is correct
55 Correct 2 ms 48508 KB Output is correct
56 Correct 2 ms 48508 KB Output is correct
57 Correct 2 ms 48508 KB Output is correct
58 Correct 2 ms 48508 KB Output is correct
59 Correct 2 ms 48508 KB Output is correct
60 Correct 2 ms 48508 KB Output is correct
61 Correct 2 ms 48508 KB Output is correct
62 Correct 2 ms 48508 KB Output is correct
63 Correct 3 ms 48508 KB Output is correct
64 Correct 184 ms 48508 KB Output is correct
65 Correct 236 ms 48508 KB Output is correct
66 Correct 261 ms 48508 KB Output is correct
67 Correct 222 ms 51272 KB Output is correct
68 Correct 36 ms 51272 KB Output is correct
69 Correct 18 ms 51272 KB Output is correct
70 Correct 18 ms 51272 KB Output is correct
71 Correct 22 ms 51272 KB Output is correct
72 Correct 24 ms 51272 KB Output is correct
73 Correct 38 ms 51272 KB Output is correct
74 Correct 214 ms 61648 KB Output is correct
75 Correct 177 ms 68232 KB Output is correct
76 Correct 194 ms 74964 KB Output is correct
77 Correct 340 ms 81724 KB Output is correct
78 Correct 176 ms 88392 KB Output is correct
79 Correct 198 ms 95260 KB Output is correct
80 Correct 201 ms 98112 KB Output is correct
81 Correct 204 ms 108840 KB Output is correct
82 Correct 179 ms 115424 KB Output is correct
83 Correct 221 ms 122100 KB Output is correct
84 Correct 223 ms 128912 KB Output is correct
85 Correct 206 ms 135544 KB Output is correct
86 Correct 216 ms 142400 KB Output is correct
87 Correct 180 ms 149076 KB Output is correct
88 Correct 213 ms 156016 KB Output is correct
89 Correct 225 ms 162568 KB Output is correct
90 Correct 209 ms 169328 KB Output is correct
91 Correct 185 ms 176280 KB Output is correct
92 Correct 253 ms 183180 KB Output is correct