#pragma GCC optimize("Ofast,unroll-loops")
#include "books.h"
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;
int n, dp[1005][1005], rp[1005], l, r, range[1005][2];
long long minimum_walk(std::vector<int> p, int s)
{
n = p.size();
l = 0, r = n - 1;
while (l < n && p[l] == l)
l++;
if (l == n)
return 0;
while (r && p[r] == r)
r--;
ll ans = 0;
if (s < l)
{
ans += 2 * (l - s);
s = l;
}
if (r < s)
{
ans += 2 * (s - r);
s = r;
}
for (int i = 0; i < n; i++)
ans += abs(p[i] - i);
for (int i = 0; i < n; i++)
rp[p[i]] = i;
cerr << "calc n\n";
// calc n
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
dp[i][j] = 1e9;
dp[s][s] = 0;
for (int i = 0; i < n; i++)
range[i][0] = min({i, p[i], rp[i]}), range[i][1] = max({i, p[i], rp[i]});
for (int l = 0; l < n; l++)
for (int i = 0; i + l < n; i++)
{
int j = i + l;
for (int k = i; k <= j; k++)
{
dp[min(i, range[k][0])][max(j, range[k][1])] = min(dp[min(i, range[k][0])][max(j, range[k][1])], dp[i][j]);
if (j + 1 < n)
dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + 2);
if (i)
dp[i - 1][j] = min(dp[i - 1][j], dp[i][j] + 2);
}
}
// for (int i = 0; i < n; i++)
// for (int j = i; j < n; j++)
// cerr << dp[i][j] << " \n"[j == n - 1];
return ans + dp[l][r];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
312 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
312 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
312 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
312 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
592 ms |
4244 KB |
Output is correct |
20 |
Correct |
585 ms |
4240 KB |
Output is correct |
21 |
Correct |
1 ms |
312 KB |
Output is correct |
22 |
Correct |
704 ms |
4244 KB |
Output is correct |
23 |
Correct |
700 ms |
4248 KB |
Output is correct |
24 |
Correct |
698 ms |
4248 KB |
Output is correct |
25 |
Correct |
701 ms |
4244 KB |
Output is correct |
26 |
Correct |
705 ms |
4240 KB |
Output is correct |
27 |
Correct |
688 ms |
4240 KB |
Output is correct |
28 |
Correct |
666 ms |
4244 KB |
Output is correct |
29 |
Correct |
676 ms |
4244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
312 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
312 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
592 ms |
4244 KB |
Output is correct |
20 |
Correct |
585 ms |
4240 KB |
Output is correct |
21 |
Correct |
1 ms |
312 KB |
Output is correct |
22 |
Correct |
704 ms |
4244 KB |
Output is correct |
23 |
Correct |
700 ms |
4248 KB |
Output is correct |
24 |
Correct |
698 ms |
4248 KB |
Output is correct |
25 |
Correct |
701 ms |
4244 KB |
Output is correct |
26 |
Correct |
705 ms |
4240 KB |
Output is correct |
27 |
Correct |
688 ms |
4240 KB |
Output is correct |
28 |
Correct |
666 ms |
4244 KB |
Output is correct |
29 |
Correct |
676 ms |
4244 KB |
Output is correct |
30 |
Runtime error |
96 ms |
25424 KB |
Execution killed with signal 11 |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
727 ms |
4248 KB |
Output is correct |
2 |
Correct |
717 ms |
4244 KB |
Output is correct |
3 |
Correct |
731 ms |
4244 KB |
Output is correct |
4 |
Correct |
706 ms |
4240 KB |
Output is correct |
5 |
Correct |
705 ms |
4248 KB |
Output is correct |
6 |
Correct |
685 ms |
4244 KB |
Output is correct |
7 |
Correct |
703 ms |
4244 KB |
Output is correct |
8 |
Correct |
711 ms |
4240 KB |
Output is correct |
9 |
Correct |
704 ms |
4240 KB |
Output is correct |
10 |
Correct |
1 ms |
308 KB |
Output is correct |
11 |
Correct |
647 ms |
4248 KB |
Output is correct |
12 |
Correct |
652 ms |
4248 KB |
Output is correct |
13 |
Correct |
686 ms |
4248 KB |
Output is correct |
14 |
Correct |
686 ms |
4244 KB |
Output is correct |
15 |
Correct |
688 ms |
4240 KB |
Output is correct |
16 |
Correct |
699 ms |
4244 KB |
Output is correct |
17 |
Correct |
676 ms |
4244 KB |
Output is correct |
18 |
Correct |
671 ms |
4244 KB |
Output is correct |
19 |
Correct |
681 ms |
4244 KB |
Output is correct |
20 |
Correct |
672 ms |
4244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
312 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
312 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
592 ms |
4244 KB |
Output is correct |
20 |
Correct |
585 ms |
4240 KB |
Output is correct |
21 |
Correct |
1 ms |
312 KB |
Output is correct |
22 |
Correct |
704 ms |
4244 KB |
Output is correct |
23 |
Correct |
700 ms |
4248 KB |
Output is correct |
24 |
Correct |
698 ms |
4248 KB |
Output is correct |
25 |
Correct |
701 ms |
4244 KB |
Output is correct |
26 |
Correct |
705 ms |
4240 KB |
Output is correct |
27 |
Correct |
688 ms |
4240 KB |
Output is correct |
28 |
Correct |
666 ms |
4244 KB |
Output is correct |
29 |
Correct |
676 ms |
4244 KB |
Output is correct |
30 |
Runtime error |
96 ms |
25424 KB |
Execution killed with signal 11 |
31 |
Halted |
0 ms |
0 KB |
- |