#include "books.h"
#include <string.h>
using namespace std;
const int N = 1000000;
const int SMALL = 1000;
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
typedef vector<int> vi;
int dd[N];
int dp[SMALL][SMALL];
long long minimum_walk(vi pp, int s) {
int n = pp.size(), i, j;
long long ans;
ans = 0;
for (i = 0; i < n; i++)
if (i < pp[i])
ans += pp[i] - i, dd[i]++, dd[pp[i]]--;
else if (i > pp[i])
ans += i - pp[i], dd[pp[i]]++, dd[i]--;
if (s == 0) {
for (i = 1; i < n - 1; i++)
dd[i] += dd[i - 1];
i = n - 1;
while (i >= 0 && pp[i] == i)
i--;
for (i--; i >= 0; i--)
if (dd[i] == 0)
ans += 2;
} else {
int l_, r_, l, r;
for (i = 0; i < n; i++)
memset(dp[i], 0x3f, n * sizeof *dp[i]);
dp[s][s] = 0;
l_ = r_ = pp[s];
for (i = s; i >= 0; i--) {
l_ = min(l_, pp[i]), r_ = max(r_, pp[i]);
l = l_, r = r_;
for (j = s; j < n; j++) {
l = min(l, pp[j]), r = max(r, pp[j]);
if (i > 0)
dp[i - 1][j] = min(dp[i - 1][j], dp[i][j] + (l >= i));
if (j + 1 < n)
dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + (r <= j));
}
}
i = 0;
while (i < s && pp[i] == i)
i++;
j = n - 1;
while (j > s && pp[j] == j)
j--;
ans += dp[i][j] * 2;
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
0 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
0 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
142 ms |
11996 KB |
Output is correct |
31 |
Correct |
144 ms |
12004 KB |
Output is correct |
32 |
Correct |
135 ms |
12004 KB |
Output is correct |
33 |
Correct |
137 ms |
12100 KB |
Output is correct |
34 |
Correct |
155 ms |
11996 KB |
Output is correct |
35 |
Correct |
145 ms |
12000 KB |
Output is correct |
36 |
Correct |
142 ms |
11972 KB |
Output is correct |
37 |
Correct |
157 ms |
11972 KB |
Output is correct |
38 |
Correct |
138 ms |
12008 KB |
Output is correct |
39 |
Correct |
137 ms |
11992 KB |
Output is correct |
40 |
Correct |
149 ms |
12024 KB |
Output is correct |
41 |
Correct |
143 ms |
12008 KB |
Output is correct |
42 |
Correct |
165 ms |
12012 KB |
Output is correct |
43 |
Correct |
150 ms |
12000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4172 KB |
Output is correct |
2 |
Correct |
2 ms |
4172 KB |
Output is correct |
3 |
Correct |
4 ms |
4172 KB |
Output is correct |
4 |
Correct |
3 ms |
4172 KB |
Output is correct |
5 |
Correct |
4 ms |
4172 KB |
Output is correct |
6 |
Correct |
4 ms |
4172 KB |
Output is correct |
7 |
Correct |
4 ms |
4172 KB |
Output is correct |
8 |
Correct |
4 ms |
4172 KB |
Output is correct |
9 |
Correct |
4 ms |
4172 KB |
Output is correct |
10 |
Correct |
3 ms |
4172 KB |
Output is correct |
11 |
Correct |
5 ms |
4172 KB |
Output is correct |
12 |
Correct |
4 ms |
4172 KB |
Output is correct |
13 |
Correct |
5 ms |
4128 KB |
Output is correct |
14 |
Correct |
5 ms |
4172 KB |
Output is correct |
15 |
Correct |
4 ms |
4300 KB |
Output is correct |
16 |
Correct |
4 ms |
4172 KB |
Output is correct |
17 |
Correct |
4 ms |
4172 KB |
Output is correct |
18 |
Correct |
3 ms |
4172 KB |
Output is correct |
19 |
Correct |
4 ms |
4172 KB |
Output is correct |
20 |
Correct |
4 ms |
4184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
0 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
142 ms |
11996 KB |
Output is correct |
31 |
Correct |
144 ms |
12004 KB |
Output is correct |
32 |
Correct |
135 ms |
12004 KB |
Output is correct |
33 |
Correct |
137 ms |
12100 KB |
Output is correct |
34 |
Correct |
155 ms |
11996 KB |
Output is correct |
35 |
Correct |
145 ms |
12000 KB |
Output is correct |
36 |
Correct |
142 ms |
11972 KB |
Output is correct |
37 |
Correct |
157 ms |
11972 KB |
Output is correct |
38 |
Correct |
138 ms |
12008 KB |
Output is correct |
39 |
Correct |
137 ms |
11992 KB |
Output is correct |
40 |
Correct |
149 ms |
12024 KB |
Output is correct |
41 |
Correct |
143 ms |
12008 KB |
Output is correct |
42 |
Correct |
165 ms |
12012 KB |
Output is correct |
43 |
Correct |
150 ms |
12000 KB |
Output is correct |
44 |
Correct |
4 ms |
4172 KB |
Output is correct |
45 |
Correct |
2 ms |
4172 KB |
Output is correct |
46 |
Correct |
4 ms |
4172 KB |
Output is correct |
47 |
Correct |
3 ms |
4172 KB |
Output is correct |
48 |
Correct |
4 ms |
4172 KB |
Output is correct |
49 |
Correct |
4 ms |
4172 KB |
Output is correct |
50 |
Correct |
4 ms |
4172 KB |
Output is correct |
51 |
Correct |
4 ms |
4172 KB |
Output is correct |
52 |
Correct |
4 ms |
4172 KB |
Output is correct |
53 |
Correct |
3 ms |
4172 KB |
Output is correct |
54 |
Correct |
5 ms |
4172 KB |
Output is correct |
55 |
Correct |
4 ms |
4172 KB |
Output is correct |
56 |
Correct |
5 ms |
4128 KB |
Output is correct |
57 |
Correct |
5 ms |
4172 KB |
Output is correct |
58 |
Correct |
4 ms |
4300 KB |
Output is correct |
59 |
Correct |
4 ms |
4172 KB |
Output is correct |
60 |
Correct |
4 ms |
4172 KB |
Output is correct |
61 |
Correct |
3 ms |
4172 KB |
Output is correct |
62 |
Correct |
4 ms |
4172 KB |
Output is correct |
63 |
Correct |
4 ms |
4184 KB |
Output is correct |
64 |
Runtime error |
304 ms |
38828 KB |
Execution killed with signal 11 |
65 |
Halted |
0 ms |
0 KB |
- |