#include <bits/stdc++.h>
using namespace std;
const int maxn = 5001;
short lsf[maxn][maxn];
int otr[maxn][maxn];
int dp[maxn];
int h[maxn];
int ih[maxn];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for(int i = 0;i < n;++i)
{
cin >> h[i];
h[i]--;
ih[h[i]] = i;
}
for(int i = 0;i < n;++i)
{
for(int j = n-1;j >= 0;--j)
{
lsf[i][j] = (j == n-1 ? ih[i] > ih[j] : (ih[i] > ih[j]) + lsf[i][j+1]);
}
}
for(int l = 0;l < n;++l)
{
for(int r = l;r < n;++r)
{
otr[l][r] = (l == r ? lsf[l][l] : lsf[r][l]*2 - lsf[r][r] - r + l + otr[l][r-1]);
}
}
dp[n] = 0;
for(int i = n-1;i >= 0;--i)
{
dp[i] = 1e9;
for(int j = i+1;j <= n;++j)
{
dp[i] = min(dp[i],dp[j] + otr[i][j-1]);
}
}
cout << dp[0];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4440 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4448 KB |
Output is correct |
14 |
Correct |
1 ms |
4572 KB |
Output is correct |
15 |
Correct |
1 ms |
4440 KB |
Output is correct |
16 |
Correct |
2 ms |
4564 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4448 KB |
Output is correct |
19 |
Correct |
1 ms |
4448 KB |
Output is correct |
20 |
Correct |
1 ms |
4448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4440 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4448 KB |
Output is correct |
14 |
Correct |
1 ms |
4572 KB |
Output is correct |
15 |
Correct |
1 ms |
4440 KB |
Output is correct |
16 |
Correct |
2 ms |
4564 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4448 KB |
Output is correct |
19 |
Correct |
1 ms |
4448 KB |
Output is correct |
20 |
Correct |
1 ms |
4448 KB |
Output is correct |
21 |
Correct |
2 ms |
8544 KB |
Output is correct |
22 |
Correct |
2 ms |
8544 KB |
Output is correct |
23 |
Correct |
2 ms |
8544 KB |
Output is correct |
24 |
Correct |
2 ms |
8544 KB |
Output is correct |
25 |
Correct |
2 ms |
8540 KB |
Output is correct |
26 |
Correct |
2 ms |
8544 KB |
Output is correct |
27 |
Correct |
2 ms |
8544 KB |
Output is correct |
28 |
Correct |
2 ms |
8696 KB |
Output is correct |
29 |
Correct |
2 ms |
8544 KB |
Output is correct |
30 |
Correct |
2 ms |
8544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4440 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4448 KB |
Output is correct |
14 |
Correct |
1 ms |
4572 KB |
Output is correct |
15 |
Correct |
1 ms |
4440 KB |
Output is correct |
16 |
Correct |
2 ms |
4564 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4448 KB |
Output is correct |
19 |
Correct |
1 ms |
4448 KB |
Output is correct |
20 |
Correct |
1 ms |
4448 KB |
Output is correct |
21 |
Correct |
2 ms |
8544 KB |
Output is correct |
22 |
Correct |
2 ms |
8544 KB |
Output is correct |
23 |
Correct |
2 ms |
8544 KB |
Output is correct |
24 |
Correct |
2 ms |
8544 KB |
Output is correct |
25 |
Correct |
2 ms |
8540 KB |
Output is correct |
26 |
Correct |
2 ms |
8544 KB |
Output is correct |
27 |
Correct |
2 ms |
8544 KB |
Output is correct |
28 |
Correct |
2 ms |
8696 KB |
Output is correct |
29 |
Correct |
2 ms |
8544 KB |
Output is correct |
30 |
Correct |
2 ms |
8544 KB |
Output is correct |
31 |
Correct |
8 ms |
26976 KB |
Output is correct |
32 |
Correct |
8 ms |
26976 KB |
Output is correct |
33 |
Correct |
8 ms |
26980 KB |
Output is correct |
34 |
Correct |
7 ms |
27088 KB |
Output is correct |
35 |
Correct |
7 ms |
26972 KB |
Output is correct |
36 |
Correct |
7 ms |
26972 KB |
Output is correct |
37 |
Correct |
8 ms |
26968 KB |
Output is correct |
38 |
Correct |
8 ms |
26968 KB |
Output is correct |
39 |
Correct |
7 ms |
26972 KB |
Output is correct |
40 |
Correct |
7 ms |
26968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4440 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4448 KB |
Output is correct |
14 |
Correct |
1 ms |
4572 KB |
Output is correct |
15 |
Correct |
1 ms |
4440 KB |
Output is correct |
16 |
Correct |
2 ms |
4564 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4448 KB |
Output is correct |
19 |
Correct |
1 ms |
4448 KB |
Output is correct |
20 |
Correct |
1 ms |
4448 KB |
Output is correct |
21 |
Correct |
2 ms |
8544 KB |
Output is correct |
22 |
Correct |
2 ms |
8544 KB |
Output is correct |
23 |
Correct |
2 ms |
8544 KB |
Output is correct |
24 |
Correct |
2 ms |
8544 KB |
Output is correct |
25 |
Correct |
2 ms |
8540 KB |
Output is correct |
26 |
Correct |
2 ms |
8544 KB |
Output is correct |
27 |
Correct |
2 ms |
8544 KB |
Output is correct |
28 |
Correct |
2 ms |
8696 KB |
Output is correct |
29 |
Correct |
2 ms |
8544 KB |
Output is correct |
30 |
Correct |
2 ms |
8544 KB |
Output is correct |
31 |
Correct |
8 ms |
26976 KB |
Output is correct |
32 |
Correct |
8 ms |
26976 KB |
Output is correct |
33 |
Correct |
8 ms |
26980 KB |
Output is correct |
34 |
Correct |
7 ms |
27088 KB |
Output is correct |
35 |
Correct |
7 ms |
26972 KB |
Output is correct |
36 |
Correct |
7 ms |
26972 KB |
Output is correct |
37 |
Correct |
8 ms |
26968 KB |
Output is correct |
38 |
Correct |
8 ms |
26968 KB |
Output is correct |
39 |
Correct |
7 ms |
26972 KB |
Output is correct |
40 |
Correct |
7 ms |
26968 KB |
Output is correct |
41 |
Correct |
154 ms |
117644 KB |
Output is correct |
42 |
Correct |
155 ms |
117244 KB |
Output is correct |
43 |
Correct |
154 ms |
117332 KB |
Output is correct |
44 |
Correct |
152 ms |
117340 KB |
Output is correct |
45 |
Correct |
157 ms |
117328 KB |
Output is correct |
46 |
Correct |
153 ms |
117328 KB |
Output is correct |
47 |
Correct |
156 ms |
117308 KB |
Output is correct |
48 |
Correct |
160 ms |
117540 KB |
Output is correct |
49 |
Correct |
151 ms |
117332 KB |
Output is correct |
50 |
Correct |
154 ms |
117280 KB |
Output is correct |
51 |
Correct |
153 ms |
117328 KB |
Output is correct |
52 |
Correct |
154 ms |
117328 KB |
Output is correct |
53 |
Correct |
153 ms |
117332 KB |
Output is correct |
54 |
Correct |
157 ms |
117580 KB |
Output is correct |
55 |
Correct |
154 ms |
117584 KB |
Output is correct |