#include "bits/stdc++.h"
#include <vector>
using namespace std;
using ll = long long;
const int INF = 1e9;
int n;
int a[5005];
vector<vector<int>> p1, p2;
void compute_p(vector<vector<int>>& p) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
p[i][j] = p[i][j] + p[i][j - 1] + p[i - 1][j] - p[i - 1][j - 1];
}
}
}
int get_p(vector<vector<int>>& p, int l1, int r1, int l2, int r2) {
return p[r1][r2] - p[l1 - 1][r2] - p[r1][l2 - 1] + p[l1 - 1][l2 - 1];
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
p1 = vector<vector<int>>(n + 5, vector<int>(n + 5, 0));
p2 = vector<vector<int>>(n + 5, vector<int>(n + 5, 0));
vector<int> dp(n + 5, INF);
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
if (a[i] > a[j]) {
++p1[a[i]][a[j]];
}
else if (a[i] < a[j]) {
++p2[a[i]][a[j]];
}
}
}
// for (int i = 1; i <= n; i++) {
// for (int j = 1; j <= n; j++) {
// cout << p1[i][j] << " \n"[j == n];
// }
// }
// cout << "\n";
// for (int i = 1; i <= n; i++) {
// for (int j = 1; j <= n; j++) {
// cout << p2[i][j] << " \n"[j == n];
// }
// }
compute_p(p1);
compute_p(p2);
// cout << "===\n";
// for (int i = 1; i <= n; i++) {
// for (int j = 1; j <= n; j++) {
// cout << p1[i][j] << " \n"[j == n];
// }
// }
// cout << "\n";
// for (int i = 1; i <= n; i++) {
// for (int j = 1; j <= n; j++) {
// cout << p2[i][j] << " \n"[j == n];
// }
// }
dp[0] = 0;
for (int i = 1; i <= n; i++) {
{
int cost2 = get_p(p2, 1, i, 1, i);
dp[i] = min(dp[i], cost2);
}
for (int j = 1; j < i; j++) {
int cost1 = get_p(p1, j + 1, i, 1, j);
int cost2 = get_p(p2, j + 1, i, j + 1, i);
dp[i] = min(dp[i], cost1 + cost2 + dp[j]);
}
}
cout << dp[n] << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
10 ms |
5332 KB |
Output is correct |
32 |
Correct |
11 ms |
5460 KB |
Output is correct |
33 |
Correct |
10 ms |
5460 KB |
Output is correct |
34 |
Correct |
10 ms |
5460 KB |
Output is correct |
35 |
Correct |
10 ms |
5448 KB |
Output is correct |
36 |
Correct |
10 ms |
5460 KB |
Output is correct |
37 |
Correct |
10 ms |
5448 KB |
Output is correct |
38 |
Correct |
10 ms |
5460 KB |
Output is correct |
39 |
Correct |
10 ms |
5460 KB |
Output is correct |
40 |
Correct |
10 ms |
5460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
10 ms |
5332 KB |
Output is correct |
32 |
Correct |
11 ms |
5460 KB |
Output is correct |
33 |
Correct |
10 ms |
5460 KB |
Output is correct |
34 |
Correct |
10 ms |
5460 KB |
Output is correct |
35 |
Correct |
10 ms |
5448 KB |
Output is correct |
36 |
Correct |
10 ms |
5460 KB |
Output is correct |
37 |
Correct |
10 ms |
5448 KB |
Output is correct |
38 |
Correct |
10 ms |
5460 KB |
Output is correct |
39 |
Correct |
10 ms |
5460 KB |
Output is correct |
40 |
Correct |
10 ms |
5460 KB |
Output is correct |
41 |
Correct |
638 ms |
196580 KB |
Output is correct |
42 |
Correct |
646 ms |
196740 KB |
Output is correct |
43 |
Correct |
654 ms |
196812 KB |
Output is correct |
44 |
Correct |
680 ms |
196684 KB |
Output is correct |
45 |
Correct |
700 ms |
196768 KB |
Output is correct |
46 |
Correct |
654 ms |
196832 KB |
Output is correct |
47 |
Correct |
654 ms |
196868 KB |
Output is correct |
48 |
Correct |
686 ms |
196804 KB |
Output is correct |
49 |
Correct |
733 ms |
196804 KB |
Output is correct |
50 |
Correct |
738 ms |
196804 KB |
Output is correct |
51 |
Correct |
726 ms |
196808 KB |
Output is correct |
52 |
Correct |
743 ms |
196684 KB |
Output is correct |
53 |
Correct |
772 ms |
196808 KB |
Output is correct |
54 |
Correct |
696 ms |
196896 KB |
Output is correct |
55 |
Correct |
732 ms |
196804 KB |
Output is correct |