#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 |
1 ms |
224 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
224 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
212 KB |
Output is correct |
16 |
Correct |
2 ms |
328 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
328 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
224 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
212 KB |
Output is correct |
16 |
Correct |
2 ms |
328 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
328 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
998 ms |
980 KB |
Output is correct |
22 |
Correct |
1106 ms |
820 KB |
Output is correct |
23 |
Correct |
1129 ms |
1076 KB |
Output is correct |
24 |
Correct |
1155 ms |
912 KB |
Output is correct |
25 |
Correct |
1513 ms |
840 KB |
Output is correct |
26 |
Correct |
1130 ms |
876 KB |
Output is correct |
27 |
Correct |
1033 ms |
920 KB |
Output is correct |
28 |
Correct |
1024 ms |
916 KB |
Output is correct |
29 |
Correct |
1033 ms |
836 KB |
Output is correct |
30 |
Correct |
1171 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
224 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
212 KB |
Output is correct |
16 |
Correct |
2 ms |
328 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
328 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
998 ms |
980 KB |
Output is correct |
22 |
Correct |
1106 ms |
820 KB |
Output is correct |
23 |
Correct |
1129 ms |
1076 KB |
Output is correct |
24 |
Correct |
1155 ms |
912 KB |
Output is correct |
25 |
Correct |
1513 ms |
840 KB |
Output is correct |
26 |
Correct |
1130 ms |
876 KB |
Output is correct |
27 |
Correct |
1033 ms |
920 KB |
Output is correct |
28 |
Correct |
1024 ms |
916 KB |
Output is correct |
29 |
Correct |
1033 ms |
836 KB |
Output is correct |
30 |
Correct |
1171 ms |
860 KB |
Output is correct |
31 |
Execution timed out |
5067 ms |
8220 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
224 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
212 KB |
Output is correct |
16 |
Correct |
2 ms |
328 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
328 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
998 ms |
980 KB |
Output is correct |
22 |
Correct |
1106 ms |
820 KB |
Output is correct |
23 |
Correct |
1129 ms |
1076 KB |
Output is correct |
24 |
Correct |
1155 ms |
912 KB |
Output is correct |
25 |
Correct |
1513 ms |
840 KB |
Output is correct |
26 |
Correct |
1130 ms |
876 KB |
Output is correct |
27 |
Correct |
1033 ms |
920 KB |
Output is correct |
28 |
Correct |
1024 ms |
916 KB |
Output is correct |
29 |
Correct |
1033 ms |
836 KB |
Output is correct |
30 |
Correct |
1171 ms |
860 KB |
Output is correct |
31 |
Execution timed out |
5067 ms |
8220 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |