#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 5000;
int h[MAX_N + 1], p[MAX_N + 1], lower[MAX_N + 1][MAX_N + 1], costSmall[MAX_N + 1][MAX_N + 1], costBig[MAX_N + 1][MAX_N + 1], minCost[MAX_N + 1];
int main() {
int n;
cin >> n;
for ( int i = 1; i <= n; i++ ) {
cin >> h[i];
p[h[i]] = i;
}
for ( int i = 1; i <= n; i++ ) {
for ( int j = 1; j < i; j++ )
lower[i][h[j]]++;
for ( int j = 1; j <= n; j++ )
lower[i][j] += lower[i][j - 1];
}
for ( int l = 1; l <= n; l++ ) {
for ( int r = l; r <= n; r++ )
costSmall[l][r] = costSmall[l][r - 1] + lower[p[r]][r - 1] - lower[p[r]][l - 1];
}
for ( int r = 1; r <= n; r++ ) {
for ( int l = r; l >= 1; l--)
costBig[l][r] = costBig[l + 1][r] + lower[p[l]][n] - lower[p[l]][r];
}
for ( int i = 1; i <= n; i++ ) {
minCost[i] = n * n;
for ( int j = 0; j < i; j++ )
minCost[i] = min( minCost[i], minCost[j] + costSmall[j + 1][i] + costBig[j + 1][i] );
}
cout << minCost[n] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
1 ms |
592 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
1 ms |
592 KB |
Output is correct |
16 |
Correct |
1 ms |
592 KB |
Output is correct |
17 |
Correct |
1 ms |
592 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
1 ms |
592 KB |
Output is correct |
20 |
Correct |
1 ms |
592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
1 ms |
592 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
1 ms |
592 KB |
Output is correct |
16 |
Correct |
1 ms |
592 KB |
Output is correct |
17 |
Correct |
1 ms |
592 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
1 ms |
592 KB |
Output is correct |
20 |
Correct |
1 ms |
592 KB |
Output is correct |
21 |
Correct |
3 ms |
6480 KB |
Output is correct |
22 |
Correct |
3 ms |
6480 KB |
Output is correct |
23 |
Correct |
3 ms |
6480 KB |
Output is correct |
24 |
Correct |
3 ms |
6480 KB |
Output is correct |
25 |
Correct |
3 ms |
6480 KB |
Output is correct |
26 |
Correct |
3 ms |
6480 KB |
Output is correct |
27 |
Correct |
3 ms |
6652 KB |
Output is correct |
28 |
Correct |
3 ms |
6556 KB |
Output is correct |
29 |
Correct |
3 ms |
6480 KB |
Output is correct |
30 |
Correct |
3 ms |
6480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
1 ms |
592 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
1 ms |
592 KB |
Output is correct |
16 |
Correct |
1 ms |
592 KB |
Output is correct |
17 |
Correct |
1 ms |
592 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
1 ms |
592 KB |
Output is correct |
20 |
Correct |
1 ms |
592 KB |
Output is correct |
21 |
Correct |
3 ms |
6480 KB |
Output is correct |
22 |
Correct |
3 ms |
6480 KB |
Output is correct |
23 |
Correct |
3 ms |
6480 KB |
Output is correct |
24 |
Correct |
3 ms |
6480 KB |
Output is correct |
25 |
Correct |
3 ms |
6480 KB |
Output is correct |
26 |
Correct |
3 ms |
6480 KB |
Output is correct |
27 |
Correct |
3 ms |
6652 KB |
Output is correct |
28 |
Correct |
3 ms |
6556 KB |
Output is correct |
29 |
Correct |
3 ms |
6480 KB |
Output is correct |
30 |
Correct |
3 ms |
6480 KB |
Output is correct |
31 |
Correct |
12 ms |
24144 KB |
Output is correct |
32 |
Correct |
13 ms |
24144 KB |
Output is correct |
33 |
Correct |
13 ms |
24156 KB |
Output is correct |
34 |
Correct |
12 ms |
24144 KB |
Output is correct |
35 |
Correct |
14 ms |
24144 KB |
Output is correct |
36 |
Correct |
12 ms |
24144 KB |
Output is correct |
37 |
Correct |
13 ms |
24144 KB |
Output is correct |
38 |
Correct |
12 ms |
24144 KB |
Output is correct |
39 |
Correct |
14 ms |
24144 KB |
Output is correct |
40 |
Correct |
12 ms |
24144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
1 ms |
592 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
1 ms |
592 KB |
Output is correct |
16 |
Correct |
1 ms |
592 KB |
Output is correct |
17 |
Correct |
1 ms |
592 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
1 ms |
592 KB |
Output is correct |
20 |
Correct |
1 ms |
592 KB |
Output is correct |
21 |
Correct |
3 ms |
6480 KB |
Output is correct |
22 |
Correct |
3 ms |
6480 KB |
Output is correct |
23 |
Correct |
3 ms |
6480 KB |
Output is correct |
24 |
Correct |
3 ms |
6480 KB |
Output is correct |
25 |
Correct |
3 ms |
6480 KB |
Output is correct |
26 |
Correct |
3 ms |
6480 KB |
Output is correct |
27 |
Correct |
3 ms |
6652 KB |
Output is correct |
28 |
Correct |
3 ms |
6556 KB |
Output is correct |
29 |
Correct |
3 ms |
6480 KB |
Output is correct |
30 |
Correct |
3 ms |
6480 KB |
Output is correct |
31 |
Correct |
12 ms |
24144 KB |
Output is correct |
32 |
Correct |
13 ms |
24144 KB |
Output is correct |
33 |
Correct |
13 ms |
24156 KB |
Output is correct |
34 |
Correct |
12 ms |
24144 KB |
Output is correct |
35 |
Correct |
14 ms |
24144 KB |
Output is correct |
36 |
Correct |
12 ms |
24144 KB |
Output is correct |
37 |
Correct |
13 ms |
24144 KB |
Output is correct |
38 |
Correct |
12 ms |
24144 KB |
Output is correct |
39 |
Correct |
14 ms |
24144 KB |
Output is correct |
40 |
Correct |
12 ms |
24144 KB |
Output is correct |
41 |
Correct |
335 ms |
242496 KB |
Output is correct |
42 |
Correct |
338 ms |
242204 KB |
Output is correct |
43 |
Correct |
336 ms |
242376 KB |
Output is correct |
44 |
Correct |
334 ms |
242248 KB |
Output is correct |
45 |
Correct |
334 ms |
242372 KB |
Output is correct |
46 |
Correct |
349 ms |
242368 KB |
Output is correct |
47 |
Correct |
339 ms |
242248 KB |
Output is correct |
48 |
Correct |
351 ms |
242248 KB |
Output is correct |
49 |
Correct |
345 ms |
242248 KB |
Output is correct |
50 |
Correct |
329 ms |
242296 KB |
Output is correct |
51 |
Correct |
351 ms |
242360 KB |
Output is correct |
52 |
Correct |
333 ms |
242436 KB |
Output is correct |
53 |
Correct |
329 ms |
242208 KB |
Output is correct |
54 |
Correct |
330 ms |
242248 KB |
Output is correct |
55 |
Correct |
309 ms |
242248 KB |
Output is correct |