#include <bits/stdc++.h>
int main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
int n;
std::cin >> n;
std::vector<int> h(n);
for (auto& i : h) std::cin >> i;
std::vector<int> pos(n);
for (int i = 0; i < n; ++i) pos[h[i] - 1] = i;
std::vector<std::vector<int>> pref(n + 1, std::vector<int>(n + 1));
for (int i = 0; i < n; ++i) {
pref[i + 1] = pref[i];
for (int j = 0; j <= n; ++j) {
if (h[i] <= j) pref[i + 1][j]++;
}
}
std::vector<int> dp(n + 1, 1e9);
dp[0] = 0;
for (int i = 0; i < n; ++i) {
int cur = dp[i];
for (int j = i; j < n; ++j) {
int k = pos[j];
int add = k - i;
add += pref[n][j] - pref[k][j];
add -= 2 * ((pref[n][j] - pref[n][i]) - (pref[k][j] - pref[k][i]));
cur += add;
dp[j + 1] = std::min(dp[j + 1], cur);
}
}
std::cout << dp[n];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
500 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
500 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
500 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
452 KB |
Output is correct |
24 |
Correct |
0 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
856 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
0 ms |
604 KB |
Output is correct |
30 |
Correct |
0 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
500 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
452 KB |
Output is correct |
24 |
Correct |
0 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
856 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
0 ms |
604 KB |
Output is correct |
30 |
Correct |
0 ms |
604 KB |
Output is correct |
31 |
Correct |
3 ms |
2904 KB |
Output is correct |
32 |
Correct |
3 ms |
2904 KB |
Output is correct |
33 |
Correct |
3 ms |
2908 KB |
Output is correct |
34 |
Correct |
3 ms |
2908 KB |
Output is correct |
35 |
Correct |
4 ms |
2904 KB |
Output is correct |
36 |
Correct |
3 ms |
2908 KB |
Output is correct |
37 |
Correct |
3 ms |
2908 KB |
Output is correct |
38 |
Correct |
3 ms |
2908 KB |
Output is correct |
39 |
Correct |
3 ms |
2908 KB |
Output is correct |
40 |
Correct |
3 ms |
2908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
500 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
452 KB |
Output is correct |
24 |
Correct |
0 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
856 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
0 ms |
604 KB |
Output is correct |
30 |
Correct |
0 ms |
604 KB |
Output is correct |
31 |
Correct |
3 ms |
2904 KB |
Output is correct |
32 |
Correct |
3 ms |
2904 KB |
Output is correct |
33 |
Correct |
3 ms |
2908 KB |
Output is correct |
34 |
Correct |
3 ms |
2908 KB |
Output is correct |
35 |
Correct |
4 ms |
2904 KB |
Output is correct |
36 |
Correct |
3 ms |
2908 KB |
Output is correct |
37 |
Correct |
3 ms |
2908 KB |
Output is correct |
38 |
Correct |
3 ms |
2908 KB |
Output is correct |
39 |
Correct |
3 ms |
2908 KB |
Output is correct |
40 |
Correct |
3 ms |
2908 KB |
Output is correct |
41 |
Correct |
336 ms |
98644 KB |
Output is correct |
42 |
Correct |
328 ms |
98540 KB |
Output is correct |
43 |
Correct |
333 ms |
98564 KB |
Output is correct |
44 |
Correct |
327 ms |
98560 KB |
Output is correct |
45 |
Correct |
324 ms |
98384 KB |
Output is correct |
46 |
Correct |
363 ms |
98576 KB |
Output is correct |
47 |
Correct |
337 ms |
98580 KB |
Output is correct |
48 |
Correct |
324 ms |
98384 KB |
Output is correct |
49 |
Correct |
355 ms |
98572 KB |
Output is correct |
50 |
Correct |
369 ms |
98396 KB |
Output is correct |
51 |
Correct |
324 ms |
98580 KB |
Output is correct |
52 |
Correct |
324 ms |
98388 KB |
Output is correct |
53 |
Correct |
337 ms |
98576 KB |
Output is correct |
54 |
Correct |
317 ms |
98384 KB |
Output is correct |
55 |
Correct |
324 ms |
98552 KB |
Output is correct |