#include <bits/stdc++.h>
using namespace std;
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n;
cin >> n;
vector<int> ind(n);
for (int i = 0; i < n; ++i) {
int ai;
cin >> ai;
--ai;
ind[ai] = i;
}
vector<vector<int>> invprefsum(n, vector<int>(n)), antiinvprefsum(n, vector<int>(n));
for (int a = 0; a < n; ++a) {
for (int b = a + 1; b < n; ++b) {
if (ind[b] < ind[a])
invprefsum[a][b] = 1;
else
antiinvprefsum[a][b] = 1;
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i) {
invprefsum[i][j] += invprefsum[i - 1][j];
antiinvprefsum[i][j] += antiinvprefsum[i - 1][j];
}
if (j) {
invprefsum[i][j] += invprefsum[i][j - 1];
antiinvprefsum[i][j] += antiinvprefsum[i][j - 1];
}
if (i && j) {
invprefsum[i][j] -= invprefsum[i - 1][j - 1];
antiinvprefsum[i][j] -= antiinvprefsum[i - 1][j - 1];
}
}
}
auto cinv = [&](int l1, int r1, int l2, int r2) {
int ans = invprefsum[r1][r2];
if (l1)
ans -= invprefsum[l1 - 1][r2];
if (l2)
ans -= invprefsum[r1][l2 - 1];
if (l1 && l2)
ans += invprefsum[l1 - 1][l2 - 1];
return ans;
};
auto cantiinv = [&](int l1, int r1, int l2, int r2) {
int ans = antiinvprefsum[r1][r2];
if (l1)
ans -= antiinvprefsum[l1 - 1][r2];
if (l2)
ans -= antiinvprefsum[r1][l2 - 1];
if (l1 && l2)
ans += antiinvprefsum[l1 - 1][l2 - 1];
return ans;
};
vector<int> dp(n);
for (int i = 1; i < n; ++i) {
dp[i] = cantiinv(0, i, 0, i);
for (int j = i; j > 0; --j) {
int newval = cantiinv(j, i, j, i);
newval += dp[j - 1];
newval += cinv(0, j - 1, j, i);
dp[i] = min(dp[i], newval);
}
}
cout << dp.back();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
456 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
452 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
456 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
452 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
456 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
600 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
1 ms |
708 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
456 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
452 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
456 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
600 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
1 ms |
708 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
7 ms |
5468 KB |
Output is correct |
32 |
Correct |
7 ms |
5468 KB |
Output is correct |
33 |
Correct |
8 ms |
5624 KB |
Output is correct |
34 |
Correct |
7 ms |
5468 KB |
Output is correct |
35 |
Correct |
7 ms |
5468 KB |
Output is correct |
36 |
Correct |
7 ms |
5468 KB |
Output is correct |
37 |
Correct |
7 ms |
5468 KB |
Output is correct |
38 |
Correct |
8 ms |
5468 KB |
Output is correct |
39 |
Correct |
7 ms |
5540 KB |
Output is correct |
40 |
Correct |
8 ms |
5468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
456 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
452 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
456 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
600 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
1 ms |
708 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
7 ms |
5468 KB |
Output is correct |
32 |
Correct |
7 ms |
5468 KB |
Output is correct |
33 |
Correct |
8 ms |
5624 KB |
Output is correct |
34 |
Correct |
7 ms |
5468 KB |
Output is correct |
35 |
Correct |
7 ms |
5468 KB |
Output is correct |
36 |
Correct |
7 ms |
5468 KB |
Output is correct |
37 |
Correct |
7 ms |
5468 KB |
Output is correct |
38 |
Correct |
8 ms |
5468 KB |
Output is correct |
39 |
Correct |
7 ms |
5540 KB |
Output is correct |
40 |
Correct |
8 ms |
5468 KB |
Output is correct |
41 |
Correct |
547 ms |
196308 KB |
Output is correct |
42 |
Correct |
553 ms |
196176 KB |
Output is correct |
43 |
Correct |
710 ms |
196720 KB |
Output is correct |
44 |
Correct |
695 ms |
196540 KB |
Output is correct |
45 |
Correct |
702 ms |
196544 KB |
Output is correct |
46 |
Correct |
550 ms |
196584 KB |
Output is correct |
47 |
Correct |
547 ms |
196576 KB |
Output is correct |
48 |
Correct |
556 ms |
196676 KB |
Output is correct |
49 |
Correct |
547 ms |
196588 KB |
Output is correct |
50 |
Correct |
545 ms |
196436 KB |
Output is correct |
51 |
Correct |
542 ms |
196804 KB |
Output is correct |
52 |
Correct |
542 ms |
196580 KB |
Output is correct |
53 |
Correct |
548 ms |
196584 KB |
Output is correct |
54 |
Correct |
545 ms |
196568 KB |
Output is correct |
55 |
Correct |
554 ms |
196688 KB |
Output is correct |