#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], cost[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++ ) {
for ( int i = 1; i <= n; i++ ) {
if ( l <= h[i] && h[i] <= r )
cost[l][r] += lower[i][h[i] - 1] - lower[i][l - 1] + lower[i][n] - lower[i][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] + cost[j + 1][i] );
}
cout << minCost[n] << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
504 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 |
376 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
2 ms |
2384 KB |
Output is correct |
8 |
Correct |
2 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
504 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 |
376 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
2 ms |
2384 KB |
Output is correct |
8 |
Correct |
2 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
2 ms |
2640 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
13 |
Correct |
2 ms |
2384 KB |
Output is correct |
14 |
Correct |
2 ms |
2384 KB |
Output is correct |
15 |
Correct |
1 ms |
2384 KB |
Output is correct |
16 |
Correct |
1 ms |
2384 KB |
Output is correct |
17 |
Correct |
1 ms |
2384 KB |
Output is correct |
18 |
Correct |
1 ms |
2384 KB |
Output is correct |
19 |
Correct |
1 ms |
2384 KB |
Output is correct |
20 |
Correct |
1 ms |
2640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
504 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 |
376 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
2 ms |
2384 KB |
Output is correct |
8 |
Correct |
2 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
2 ms |
2640 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
13 |
Correct |
2 ms |
2384 KB |
Output is correct |
14 |
Correct |
2 ms |
2384 KB |
Output is correct |
15 |
Correct |
1 ms |
2384 KB |
Output is correct |
16 |
Correct |
1 ms |
2384 KB |
Output is correct |
17 |
Correct |
1 ms |
2384 KB |
Output is correct |
18 |
Correct |
1 ms |
2384 KB |
Output is correct |
19 |
Correct |
1 ms |
2384 KB |
Output is correct |
20 |
Correct |
1 ms |
2640 KB |
Output is correct |
21 |
Correct |
7 ms |
8784 KB |
Output is correct |
22 |
Correct |
8 ms |
8784 KB |
Output is correct |
23 |
Correct |
7 ms |
8784 KB |
Output is correct |
24 |
Correct |
7 ms |
8872 KB |
Output is correct |
25 |
Correct |
7 ms |
8784 KB |
Output is correct |
26 |
Correct |
8 ms |
8784 KB |
Output is correct |
27 |
Correct |
7 ms |
8784 KB |
Output is correct |
28 |
Correct |
7 ms |
8784 KB |
Output is correct |
29 |
Correct |
7 ms |
8784 KB |
Output is correct |
30 |
Correct |
7 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
504 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 |
376 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
2 ms |
2384 KB |
Output is correct |
8 |
Correct |
2 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
2 ms |
2640 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
13 |
Correct |
2 ms |
2384 KB |
Output is correct |
14 |
Correct |
2 ms |
2384 KB |
Output is correct |
15 |
Correct |
1 ms |
2384 KB |
Output is correct |
16 |
Correct |
1 ms |
2384 KB |
Output is correct |
17 |
Correct |
1 ms |
2384 KB |
Output is correct |
18 |
Correct |
1 ms |
2384 KB |
Output is correct |
19 |
Correct |
1 ms |
2384 KB |
Output is correct |
20 |
Correct |
1 ms |
2640 KB |
Output is correct |
21 |
Correct |
7 ms |
8784 KB |
Output is correct |
22 |
Correct |
8 ms |
8784 KB |
Output is correct |
23 |
Correct |
7 ms |
8784 KB |
Output is correct |
24 |
Correct |
7 ms |
8872 KB |
Output is correct |
25 |
Correct |
7 ms |
8784 KB |
Output is correct |
26 |
Correct |
8 ms |
8784 KB |
Output is correct |
27 |
Correct |
7 ms |
8784 KB |
Output is correct |
28 |
Correct |
7 ms |
8784 KB |
Output is correct |
29 |
Correct |
7 ms |
8784 KB |
Output is correct |
30 |
Correct |
7 ms |
8796 KB |
Output is correct |
31 |
Correct |
393 ms |
33608 KB |
Output is correct |
32 |
Correct |
391 ms |
33596 KB |
Output is correct |
33 |
Correct |
369 ms |
33864 KB |
Output is correct |
34 |
Correct |
364 ms |
33612 KB |
Output is correct |
35 |
Correct |
371 ms |
33608 KB |
Output is correct |
36 |
Correct |
365 ms |
33612 KB |
Output is correct |
37 |
Correct |
365 ms |
33604 KB |
Output is correct |
38 |
Correct |
365 ms |
33608 KB |
Output is correct |
39 |
Correct |
364 ms |
33624 KB |
Output is correct |
40 |
Correct |
419 ms |
33604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
504 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 |
376 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
2 ms |
2384 KB |
Output is correct |
8 |
Correct |
2 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
2 ms |
2640 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
13 |
Correct |
2 ms |
2384 KB |
Output is correct |
14 |
Correct |
2 ms |
2384 KB |
Output is correct |
15 |
Correct |
1 ms |
2384 KB |
Output is correct |
16 |
Correct |
1 ms |
2384 KB |
Output is correct |
17 |
Correct |
1 ms |
2384 KB |
Output is correct |
18 |
Correct |
1 ms |
2384 KB |
Output is correct |
19 |
Correct |
1 ms |
2384 KB |
Output is correct |
20 |
Correct |
1 ms |
2640 KB |
Output is correct |
21 |
Correct |
7 ms |
8784 KB |
Output is correct |
22 |
Correct |
8 ms |
8784 KB |
Output is correct |
23 |
Correct |
7 ms |
8784 KB |
Output is correct |
24 |
Correct |
7 ms |
8872 KB |
Output is correct |
25 |
Correct |
7 ms |
8784 KB |
Output is correct |
26 |
Correct |
8 ms |
8784 KB |
Output is correct |
27 |
Correct |
7 ms |
8784 KB |
Output is correct |
28 |
Correct |
7 ms |
8784 KB |
Output is correct |
29 |
Correct |
7 ms |
8784 KB |
Output is correct |
30 |
Correct |
7 ms |
8796 KB |
Output is correct |
31 |
Correct |
393 ms |
33608 KB |
Output is correct |
32 |
Correct |
391 ms |
33596 KB |
Output is correct |
33 |
Correct |
369 ms |
33864 KB |
Output is correct |
34 |
Correct |
364 ms |
33612 KB |
Output is correct |
35 |
Correct |
371 ms |
33608 KB |
Output is correct |
36 |
Correct |
365 ms |
33612 KB |
Output is correct |
37 |
Correct |
365 ms |
33604 KB |
Output is correct |
38 |
Correct |
365 ms |
33608 KB |
Output is correct |
39 |
Correct |
364 ms |
33624 KB |
Output is correct |
40 |
Correct |
419 ms |
33604 KB |
Output is correct |
41 |
Execution timed out |
5056 ms |
100700 KB |
Time limit exceeded |
42 |
Halted |
0 ms |
0 KB |
- |