#include "books.h"
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e3 + 10;
const ll inf = 1e18;
ll used[maxn], l[maxn], r[maxn], n, dp[maxn][maxn];
int bef[maxn], aft[maxn];
ll distance(int l, int r, int i, int j)
{
if (i <= l && j >= r)
return min(l - i, j - r) * 2;
if (i > r)
return (i - r) * 2;
if (j < l)
return (l - j) * 2;
return 0;
}
long long minimum_walk(vector<int> p, int s)
{
n = p.size();
int cnt = 0;
ll ans = 0;
for (int i = 0; i < n; i ++)
{
ans = ans + abs(p[i] - i);
if (p[i] == i)
continue;
if (used[i])
continue;
cnt ++;
used[i] = cnt;
int v = p[i];
l[cnt] = i;
while(v != i)
{
used[v] = cnt;
r[cnt] = max(r[cnt], (ll)v);
v = p[v];
}
}
bef[0] = used[0];
for (int i = 1; i < n; i ++)
{
bef[i] = bef[i - 1];
if (used[i] != 0)
bef[i] = used[i];
}
aft[n - 1] = used[n - 1];
for (int i = n - 2; i >= 0; i --)
{
aft[i] = aft[i + 1];
if (used[i] != 0)
aft[i] = used[i];
}
for (int i = 0; i < n; i ++)
for (int j = 0; j < n; j ++)
dp[i][j] = inf;
dp[s][s] = 0;
if (used[s] != 0)
dp[l[used[s]]][r[used[s]]] = 0;
for (int len = 1; len <= n; len ++)
{
for (int i = 0; i < n - len + 1; i ++)
{
int j = i + len - 1;
if (dp[i][j] == inf)
continue;
vector < int > idx;
/**if (i != 0 && bef[i - 1] != 0)
idx.push_back(bef[i - 1]);
if (j != n - 1 && aft[j + 1] != 0)
idx.push_back(aft[j + 1]);*/
for (int p = 1; p <= cnt; p ++)
idx.push_back(p);
for (int cur : idx)
{
int newl = min((ll)i, l[cur]);
int newr = max((ll)j, r[cur]);
dp[newl][newr] = min(dp[newl][newr], dp[i][j] + distance(i, j, l[cur], r[cur]));
}
}
}
int lf = 0;
int rf = n - 1;
while(lf < s && used[lf] == 0)
lf ++;
while(rf > s && used[rf] == 0)
rf --;
///cout << lf << " " << rf << endl;
return ans + dp[lf][rf];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 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 |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
216 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 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 |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
216 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
4 ms |
8148 KB |
Output is correct |
20 |
Correct |
4 ms |
8148 KB |
Output is correct |
21 |
Correct |
4 ms |
8148 KB |
Output is correct |
22 |
Correct |
4 ms |
8148 KB |
Output is correct |
23 |
Correct |
6 ms |
8248 KB |
Output is correct |
24 |
Correct |
5 ms |
8148 KB |
Output is correct |
25 |
Correct |
4 ms |
8148 KB |
Output is correct |
26 |
Correct |
4 ms |
8148 KB |
Output is correct |
27 |
Correct |
5 ms |
8148 KB |
Output is correct |
28 |
Correct |
4 ms |
8148 KB |
Output is correct |
29 |
Correct |
4 ms |
8148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 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 |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
216 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
4 ms |
8148 KB |
Output is correct |
20 |
Correct |
4 ms |
8148 KB |
Output is correct |
21 |
Correct |
4 ms |
8148 KB |
Output is correct |
22 |
Correct |
4 ms |
8148 KB |
Output is correct |
23 |
Correct |
6 ms |
8248 KB |
Output is correct |
24 |
Correct |
5 ms |
8148 KB |
Output is correct |
25 |
Correct |
4 ms |
8148 KB |
Output is correct |
26 |
Correct |
4 ms |
8148 KB |
Output is correct |
27 |
Correct |
5 ms |
8148 KB |
Output is correct |
28 |
Correct |
4 ms |
8148 KB |
Output is correct |
29 |
Correct |
4 ms |
8148 KB |
Output is correct |
30 |
Runtime error |
90 ms |
23024 KB |
Execution killed with signal 11 |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
8148 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3308' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 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 |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
216 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
4 ms |
8148 KB |
Output is correct |
20 |
Correct |
4 ms |
8148 KB |
Output is correct |
21 |
Correct |
4 ms |
8148 KB |
Output is correct |
22 |
Correct |
4 ms |
8148 KB |
Output is correct |
23 |
Correct |
6 ms |
8248 KB |
Output is correct |
24 |
Correct |
5 ms |
8148 KB |
Output is correct |
25 |
Correct |
4 ms |
8148 KB |
Output is correct |
26 |
Correct |
4 ms |
8148 KB |
Output is correct |
27 |
Correct |
5 ms |
8148 KB |
Output is correct |
28 |
Correct |
4 ms |
8148 KB |
Output is correct |
29 |
Correct |
4 ms |
8148 KB |
Output is correct |
30 |
Runtime error |
90 ms |
23024 KB |
Execution killed with signal 11 |
31 |
Halted |
0 ms |
0 KB |
- |