#include "books.h"
#include<bits/stdc++.h>
using namespace std;
int N, S, p[1000009], prv[1000009], nxt[1000009], dp[1009][1009];
bool ap[1000009];
pair < int, int > jump[1009][1009];
void update (int &x, int y)
{
if (y < x) x = y;
}
long long minimum_walk (vector < int > pp, int ss)
{
N = pp.size (), S = ss + 1;
for (int i=1; i<=N; i++)
p[i] = pp[i - 1] + 1;
for (int i=1; i<=N; i++)
if (ap[i] == 0)
{
int j = p[i], maxRight = i;
ap[i] = 1;
while (j != i)
ap[j] = 1, maxRight = max (maxRight, j), j = p[j];
nxt[i] = maxRight, prv[i] = i, j = p[i];
while (j != i)
nxt[j] = maxRight, prv[j] = i, j = p[j];
}
long long ans = 0;
for (int i=1; i<=N; i++)
{
int curr = i - p[i];
if (curr < 0) curr = -curr;
ans += curr;
}
int targetL = N + 1, targetR = 0;
for (int i=1; i<=N; i++)
if (p[i] != i)
{
if (targetL == N + 1)
targetL = i;
targetR = i;
}
if (targetR == 0) return ans;
targetR = max (targetR, S);
targetL = min (targetL, S);
for (int i=1; i<=N; i++)
{
jump[i][i] = {prv[i], nxt[i]};
for (int j=i + 1; j<=N; j++)
jump[i][j].first = min (jump[i][j - 1].first, prv[j]),
jump[i][j].second = max (jump[i][j - 1].second, nxt[j]);
}
for (int i=1; i<=N; i++)
for (int j=N; j>=i; j--)
if (jump[i][j].first != i || jump[i][j].second != j)
jump[i][j] = jump[jump[i][j].first][jump[i][j].second];
for (int i=1; i<=N; i++)
for (int j=i; j<=N; j++)
dp[i][j] = 3 * N + 10;
dp[S][S] = 0;
for (int i=S; i>=targetL; i--)
for (int j=S; j<=targetR; j++)
{
if (jump[i][j].first != i || jump[i][j].second != j)
{
update (dp[jump[i][j].first][jump[i][j].second], dp[i][j]);
continue;
}
if (j < N)
update (dp[i][j + 1], dp[i][j] + 1);
if (i > 1)
update (dp[i - 1][j], dp[i][j] + 1);
}
return ans + 2LL * dp[targetL][targetR];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
432 KB |
Output is correct |
4 |
Correct |
2 ms |
432 KB |
Output is correct |
5 |
Correct |
2 ms |
484 KB |
Output is correct |
6 |
Correct |
2 ms |
540 KB |
Output is correct |
7 |
Correct |
2 ms |
560 KB |
Output is correct |
8 |
Correct |
2 ms |
560 KB |
Output is correct |
9 |
Correct |
2 ms |
616 KB |
Output is correct |
10 |
Correct |
2 ms |
616 KB |
Output is correct |
11 |
Correct |
2 ms |
616 KB |
Output is correct |
12 |
Correct |
2 ms |
636 KB |
Output is correct |
13 |
Correct |
2 ms |
636 KB |
Output is correct |
14 |
Correct |
2 ms |
636 KB |
Output is correct |
15 |
Correct |
2 ms |
636 KB |
Output is correct |
16 |
Correct |
2 ms |
636 KB |
Output is correct |
17 |
Correct |
2 ms |
636 KB |
Output is correct |
18 |
Correct |
2 ms |
636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
432 KB |
Output is correct |
4 |
Correct |
2 ms |
432 KB |
Output is correct |
5 |
Correct |
2 ms |
484 KB |
Output is correct |
6 |
Correct |
2 ms |
540 KB |
Output is correct |
7 |
Correct |
2 ms |
560 KB |
Output is correct |
8 |
Correct |
2 ms |
560 KB |
Output is correct |
9 |
Correct |
2 ms |
616 KB |
Output is correct |
10 |
Correct |
2 ms |
616 KB |
Output is correct |
11 |
Correct |
2 ms |
616 KB |
Output is correct |
12 |
Correct |
2 ms |
636 KB |
Output is correct |
13 |
Correct |
2 ms |
636 KB |
Output is correct |
14 |
Correct |
2 ms |
636 KB |
Output is correct |
15 |
Correct |
2 ms |
636 KB |
Output is correct |
16 |
Correct |
2 ms |
636 KB |
Output is correct |
17 |
Correct |
2 ms |
636 KB |
Output is correct |
18 |
Correct |
2 ms |
636 KB |
Output is correct |
19 |
Correct |
12 ms |
11484 KB |
Output is correct |
20 |
Correct |
14 ms |
11516 KB |
Output is correct |
21 |
Correct |
2 ms |
11516 KB |
Output is correct |
22 |
Correct |
14 ms |
11516 KB |
Output is correct |
23 |
Correct |
13 ms |
11516 KB |
Output is correct |
24 |
Correct |
15 ms |
11548 KB |
Output is correct |
25 |
Correct |
13 ms |
11548 KB |
Output is correct |
26 |
Correct |
12 ms |
11548 KB |
Output is correct |
27 |
Correct |
12 ms |
11548 KB |
Output is correct |
28 |
Correct |
12 ms |
11548 KB |
Output is correct |
29 |
Correct |
12 ms |
11548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
432 KB |
Output is correct |
4 |
Correct |
2 ms |
432 KB |
Output is correct |
5 |
Correct |
2 ms |
484 KB |
Output is correct |
6 |
Correct |
2 ms |
540 KB |
Output is correct |
7 |
Correct |
2 ms |
560 KB |
Output is correct |
8 |
Correct |
2 ms |
560 KB |
Output is correct |
9 |
Correct |
2 ms |
616 KB |
Output is correct |
10 |
Correct |
2 ms |
616 KB |
Output is correct |
11 |
Correct |
2 ms |
616 KB |
Output is correct |
12 |
Correct |
2 ms |
636 KB |
Output is correct |
13 |
Correct |
2 ms |
636 KB |
Output is correct |
14 |
Correct |
2 ms |
636 KB |
Output is correct |
15 |
Correct |
2 ms |
636 KB |
Output is correct |
16 |
Correct |
2 ms |
636 KB |
Output is correct |
17 |
Correct |
2 ms |
636 KB |
Output is correct |
18 |
Correct |
2 ms |
636 KB |
Output is correct |
19 |
Correct |
12 ms |
11484 KB |
Output is correct |
20 |
Correct |
14 ms |
11516 KB |
Output is correct |
21 |
Correct |
2 ms |
11516 KB |
Output is correct |
22 |
Correct |
14 ms |
11516 KB |
Output is correct |
23 |
Correct |
13 ms |
11516 KB |
Output is correct |
24 |
Correct |
15 ms |
11548 KB |
Output is correct |
25 |
Correct |
13 ms |
11548 KB |
Output is correct |
26 |
Correct |
12 ms |
11548 KB |
Output is correct |
27 |
Correct |
12 ms |
11548 KB |
Output is correct |
28 |
Correct |
12 ms |
11548 KB |
Output is correct |
29 |
Correct |
12 ms |
11548 KB |
Output is correct |
30 |
Execution timed out |
2062 ms |
32256 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
32256 KB |
Output is correct |
2 |
Correct |
13 ms |
32256 KB |
Output is correct |
3 |
Correct |
13 ms |
32256 KB |
Output is correct |
4 |
Correct |
15 ms |
32256 KB |
Output is correct |
5 |
Correct |
12 ms |
32256 KB |
Output is correct |
6 |
Correct |
13 ms |
32256 KB |
Output is correct |
7 |
Correct |
12 ms |
32256 KB |
Output is correct |
8 |
Correct |
14 ms |
32256 KB |
Output is correct |
9 |
Correct |
16 ms |
32256 KB |
Output is correct |
10 |
Correct |
2 ms |
32256 KB |
Output is correct |
11 |
Correct |
14 ms |
32256 KB |
Output is correct |
12 |
Correct |
13 ms |
32256 KB |
Output is correct |
13 |
Correct |
13 ms |
32256 KB |
Output is correct |
14 |
Correct |
13 ms |
32256 KB |
Output is correct |
15 |
Correct |
12 ms |
32256 KB |
Output is correct |
16 |
Correct |
13 ms |
32256 KB |
Output is correct |
17 |
Correct |
17 ms |
32256 KB |
Output is correct |
18 |
Correct |
12 ms |
32256 KB |
Output is correct |
19 |
Correct |
16 ms |
32256 KB |
Output is correct |
20 |
Correct |
12 ms |
32256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
432 KB |
Output is correct |
4 |
Correct |
2 ms |
432 KB |
Output is correct |
5 |
Correct |
2 ms |
484 KB |
Output is correct |
6 |
Correct |
2 ms |
540 KB |
Output is correct |
7 |
Correct |
2 ms |
560 KB |
Output is correct |
8 |
Correct |
2 ms |
560 KB |
Output is correct |
9 |
Correct |
2 ms |
616 KB |
Output is correct |
10 |
Correct |
2 ms |
616 KB |
Output is correct |
11 |
Correct |
2 ms |
616 KB |
Output is correct |
12 |
Correct |
2 ms |
636 KB |
Output is correct |
13 |
Correct |
2 ms |
636 KB |
Output is correct |
14 |
Correct |
2 ms |
636 KB |
Output is correct |
15 |
Correct |
2 ms |
636 KB |
Output is correct |
16 |
Correct |
2 ms |
636 KB |
Output is correct |
17 |
Correct |
2 ms |
636 KB |
Output is correct |
18 |
Correct |
2 ms |
636 KB |
Output is correct |
19 |
Correct |
12 ms |
11484 KB |
Output is correct |
20 |
Correct |
14 ms |
11516 KB |
Output is correct |
21 |
Correct |
2 ms |
11516 KB |
Output is correct |
22 |
Correct |
14 ms |
11516 KB |
Output is correct |
23 |
Correct |
13 ms |
11516 KB |
Output is correct |
24 |
Correct |
15 ms |
11548 KB |
Output is correct |
25 |
Correct |
13 ms |
11548 KB |
Output is correct |
26 |
Correct |
12 ms |
11548 KB |
Output is correct |
27 |
Correct |
12 ms |
11548 KB |
Output is correct |
28 |
Correct |
12 ms |
11548 KB |
Output is correct |
29 |
Correct |
12 ms |
11548 KB |
Output is correct |
30 |
Execution timed out |
2062 ms |
32256 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |