#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;
}
for (int i=1; i<N; i++)
if (nxt[i] > nxt[i + 1])
nxt[i + 1] = nxt[i];
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].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];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
412 KB |
Output is correct |
4 |
Correct |
2 ms |
428 KB |
Output is correct |
5 |
Correct |
2 ms |
428 KB |
Output is correct |
6 |
Correct |
2 ms |
492 KB |
Output is correct |
7 |
Correct |
2 ms |
688 KB |
Output is correct |
8 |
Correct |
2 ms |
688 KB |
Output is correct |
9 |
Correct |
2 ms |
688 KB |
Output is correct |
10 |
Correct |
2 ms |
688 KB |
Output is correct |
11 |
Correct |
2 ms |
688 KB |
Output is correct |
12 |
Correct |
2 ms |
688 KB |
Output is correct |
13 |
Correct |
2 ms |
688 KB |
Output is correct |
14 |
Correct |
2 ms |
704 KB |
Output is correct |
15 |
Correct |
2 ms |
704 KB |
Output is correct |
16 |
Correct |
2 ms |
704 KB |
Output is correct |
17 |
Correct |
2 ms |
704 KB |
Output is correct |
18 |
Correct |
2 ms |
704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
412 KB |
Output is correct |
4 |
Correct |
2 ms |
428 KB |
Output is correct |
5 |
Correct |
2 ms |
428 KB |
Output is correct |
6 |
Correct |
2 ms |
492 KB |
Output is correct |
7 |
Correct |
2 ms |
688 KB |
Output is correct |
8 |
Correct |
2 ms |
688 KB |
Output is correct |
9 |
Correct |
2 ms |
688 KB |
Output is correct |
10 |
Correct |
2 ms |
688 KB |
Output is correct |
11 |
Correct |
2 ms |
688 KB |
Output is correct |
12 |
Correct |
2 ms |
688 KB |
Output is correct |
13 |
Correct |
2 ms |
688 KB |
Output is correct |
14 |
Correct |
2 ms |
704 KB |
Output is correct |
15 |
Correct |
2 ms |
704 KB |
Output is correct |
16 |
Correct |
2 ms |
704 KB |
Output is correct |
17 |
Correct |
2 ms |
704 KB |
Output is correct |
18 |
Correct |
2 ms |
704 KB |
Output is correct |
19 |
Correct |
12 ms |
11516 KB |
Output is correct |
20 |
Correct |
12 ms |
11516 KB |
Output is correct |
21 |
Correct |
2 ms |
11516 KB |
Output is correct |
22 |
Correct |
11 ms |
11516 KB |
Output is correct |
23 |
Correct |
12 ms |
11592 KB |
Output is correct |
24 |
Correct |
14 ms |
11592 KB |
Output is correct |
25 |
Correct |
15 ms |
11592 KB |
Output is correct |
26 |
Correct |
12 ms |
11592 KB |
Output is correct |
27 |
Correct |
12 ms |
11592 KB |
Output is correct |
28 |
Correct |
12 ms |
11592 KB |
Output is correct |
29 |
Correct |
12 ms |
11592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
412 KB |
Output is correct |
4 |
Correct |
2 ms |
428 KB |
Output is correct |
5 |
Correct |
2 ms |
428 KB |
Output is correct |
6 |
Correct |
2 ms |
492 KB |
Output is correct |
7 |
Correct |
2 ms |
688 KB |
Output is correct |
8 |
Correct |
2 ms |
688 KB |
Output is correct |
9 |
Correct |
2 ms |
688 KB |
Output is correct |
10 |
Correct |
2 ms |
688 KB |
Output is correct |
11 |
Correct |
2 ms |
688 KB |
Output is correct |
12 |
Correct |
2 ms |
688 KB |
Output is correct |
13 |
Correct |
2 ms |
688 KB |
Output is correct |
14 |
Correct |
2 ms |
704 KB |
Output is correct |
15 |
Correct |
2 ms |
704 KB |
Output is correct |
16 |
Correct |
2 ms |
704 KB |
Output is correct |
17 |
Correct |
2 ms |
704 KB |
Output is correct |
18 |
Correct |
2 ms |
704 KB |
Output is correct |
19 |
Correct |
12 ms |
11516 KB |
Output is correct |
20 |
Correct |
12 ms |
11516 KB |
Output is correct |
21 |
Correct |
2 ms |
11516 KB |
Output is correct |
22 |
Correct |
11 ms |
11516 KB |
Output is correct |
23 |
Correct |
12 ms |
11592 KB |
Output is correct |
24 |
Correct |
14 ms |
11592 KB |
Output is correct |
25 |
Correct |
15 ms |
11592 KB |
Output is correct |
26 |
Correct |
12 ms |
11592 KB |
Output is correct |
27 |
Correct |
12 ms |
11592 KB |
Output is correct |
28 |
Correct |
12 ms |
11592 KB |
Output is correct |
29 |
Correct |
12 ms |
11592 KB |
Output is correct |
30 |
Execution timed out |
2058 ms |
32200 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
32200 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
412 KB |
Output is correct |
4 |
Correct |
2 ms |
428 KB |
Output is correct |
5 |
Correct |
2 ms |
428 KB |
Output is correct |
6 |
Correct |
2 ms |
492 KB |
Output is correct |
7 |
Correct |
2 ms |
688 KB |
Output is correct |
8 |
Correct |
2 ms |
688 KB |
Output is correct |
9 |
Correct |
2 ms |
688 KB |
Output is correct |
10 |
Correct |
2 ms |
688 KB |
Output is correct |
11 |
Correct |
2 ms |
688 KB |
Output is correct |
12 |
Correct |
2 ms |
688 KB |
Output is correct |
13 |
Correct |
2 ms |
688 KB |
Output is correct |
14 |
Correct |
2 ms |
704 KB |
Output is correct |
15 |
Correct |
2 ms |
704 KB |
Output is correct |
16 |
Correct |
2 ms |
704 KB |
Output is correct |
17 |
Correct |
2 ms |
704 KB |
Output is correct |
18 |
Correct |
2 ms |
704 KB |
Output is correct |
19 |
Correct |
12 ms |
11516 KB |
Output is correct |
20 |
Correct |
12 ms |
11516 KB |
Output is correct |
21 |
Correct |
2 ms |
11516 KB |
Output is correct |
22 |
Correct |
11 ms |
11516 KB |
Output is correct |
23 |
Correct |
12 ms |
11592 KB |
Output is correct |
24 |
Correct |
14 ms |
11592 KB |
Output is correct |
25 |
Correct |
15 ms |
11592 KB |
Output is correct |
26 |
Correct |
12 ms |
11592 KB |
Output is correct |
27 |
Correct |
12 ms |
11592 KB |
Output is correct |
28 |
Correct |
12 ms |
11592 KB |
Output is correct |
29 |
Correct |
12 ms |
11592 KB |
Output is correct |
30 |
Execution timed out |
2058 ms |
32200 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |