#include <bits/stdc++.h>
using namespace std;
#ifdef ngu
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 5005;
int n;
int h[N];
int pos[N];
int t[N];
int dp[N];
int cnt[N][N];
int cost[N][N];
int get(int l, int r) {
return l <= r ? t[r] - t[l - 1] : 0;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
#ifdef ngu
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
#endif
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> h[i];
pos[h[i]] = i;
}
for (int l = n; l >= 1; --l) {
for (int r = l + 1; r <= n; ++r) {
cnt[l][r] = cnt[l + 1][r] + cnt[l][r - 1] - cnt[l + 1][r - 1] + (pos[l] < pos[r]);
}
}
for (int r = 1; r <= n; ++r) {
fill(t + 1, t + n + 1, 0);
for (int i = r + 1; i <= n; ++i) {
t[pos[i]]++;
}
for (int i = 1; i <= n; ++i) {
t[i] += t[i - 1];
}
for (int l = r; l >= 1; --l) {
cost[l][r] = get(1, pos[l] - 1);
cost[l][r] += cost[l + 1][r];
}
for (int l = 1; l <= r; ++l) {
cost[l][r] += cnt[l][r];
}
}
for (int i = 1; i <= n; ++i) {
dp[i] = 1e9;
for (int j = 0; j < i; ++j) {
dp[i] = min(dp[i], dp[j] + cost[j + 1][i]);
}
}
cout << dp[n];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
476 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
652 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
604 KB |
Output is correct |
20 |
Correct |
0 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
476 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
652 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
604 KB |
Output is correct |
20 |
Correct |
0 ms |
604 KB |
Output is correct |
21 |
Correct |
2 ms |
2140 KB |
Output is correct |
22 |
Correct |
1 ms |
2140 KB |
Output is correct |
23 |
Correct |
2 ms |
2140 KB |
Output is correct |
24 |
Correct |
2 ms |
2140 KB |
Output is correct |
25 |
Correct |
1 ms |
2140 KB |
Output is correct |
26 |
Correct |
2 ms |
2140 KB |
Output is correct |
27 |
Correct |
1 ms |
2140 KB |
Output is correct |
28 |
Correct |
1 ms |
2140 KB |
Output is correct |
29 |
Correct |
1 ms |
2140 KB |
Output is correct |
30 |
Correct |
2 ms |
2140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
476 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
652 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
604 KB |
Output is correct |
20 |
Correct |
0 ms |
604 KB |
Output is correct |
21 |
Correct |
2 ms |
2140 KB |
Output is correct |
22 |
Correct |
1 ms |
2140 KB |
Output is correct |
23 |
Correct |
2 ms |
2140 KB |
Output is correct |
24 |
Correct |
2 ms |
2140 KB |
Output is correct |
25 |
Correct |
1 ms |
2140 KB |
Output is correct |
26 |
Correct |
2 ms |
2140 KB |
Output is correct |
27 |
Correct |
1 ms |
2140 KB |
Output is correct |
28 |
Correct |
1 ms |
2140 KB |
Output is correct |
29 |
Correct |
1 ms |
2140 KB |
Output is correct |
30 |
Correct |
2 ms |
2140 KB |
Output is correct |
31 |
Correct |
10 ms |
9184 KB |
Output is correct |
32 |
Correct |
8 ms |
9308 KB |
Output is correct |
33 |
Correct |
8 ms |
9308 KB |
Output is correct |
34 |
Correct |
8 ms |
9308 KB |
Output is correct |
35 |
Correct |
11 ms |
9304 KB |
Output is correct |
36 |
Correct |
9 ms |
9560 KB |
Output is correct |
37 |
Correct |
8 ms |
9308 KB |
Output is correct |
38 |
Correct |
8 ms |
9308 KB |
Output is correct |
39 |
Correct |
8 ms |
9304 KB |
Output is correct |
40 |
Correct |
8 ms |
9432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
476 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
652 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
604 KB |
Output is correct |
20 |
Correct |
0 ms |
604 KB |
Output is correct |
21 |
Correct |
2 ms |
2140 KB |
Output is correct |
22 |
Correct |
1 ms |
2140 KB |
Output is correct |
23 |
Correct |
2 ms |
2140 KB |
Output is correct |
24 |
Correct |
2 ms |
2140 KB |
Output is correct |
25 |
Correct |
1 ms |
2140 KB |
Output is correct |
26 |
Correct |
2 ms |
2140 KB |
Output is correct |
27 |
Correct |
1 ms |
2140 KB |
Output is correct |
28 |
Correct |
1 ms |
2140 KB |
Output is correct |
29 |
Correct |
1 ms |
2140 KB |
Output is correct |
30 |
Correct |
2 ms |
2140 KB |
Output is correct |
31 |
Correct |
10 ms |
9184 KB |
Output is correct |
32 |
Correct |
8 ms |
9308 KB |
Output is correct |
33 |
Correct |
8 ms |
9308 KB |
Output is correct |
34 |
Correct |
8 ms |
9308 KB |
Output is correct |
35 |
Correct |
11 ms |
9304 KB |
Output is correct |
36 |
Correct |
9 ms |
9560 KB |
Output is correct |
37 |
Correct |
8 ms |
9308 KB |
Output is correct |
38 |
Correct |
8 ms |
9308 KB |
Output is correct |
39 |
Correct |
8 ms |
9304 KB |
Output is correct |
40 |
Correct |
8 ms |
9432 KB |
Output is correct |
41 |
Correct |
522 ms |
134148 KB |
Output is correct |
42 |
Correct |
519 ms |
134312 KB |
Output is correct |
43 |
Correct |
508 ms |
134684 KB |
Output is correct |
44 |
Correct |
527 ms |
134488 KB |
Output is correct |
45 |
Correct |
510 ms |
134336 KB |
Output is correct |
46 |
Correct |
521 ms |
134636 KB |
Output is correct |
47 |
Correct |
501 ms |
134260 KB |
Output is correct |
48 |
Correct |
507 ms |
134528 KB |
Output is correct |
49 |
Correct |
503 ms |
134736 KB |
Output is correct |
50 |
Correct |
497 ms |
134488 KB |
Output is correct |
51 |
Correct |
511 ms |
134484 KB |
Output is correct |
52 |
Correct |
543 ms |
134488 KB |
Output is correct |
53 |
Correct |
516 ms |
134484 KB |
Output is correct |
54 |
Correct |
522 ms |
134588 KB |
Output is correct |
55 |
Correct |
505 ms |
134492 KB |
Output is correct |