#include <bits/stdc++.h>
using namespace std;
int n, dp[5100], a[5100], cost[5100][5100] = {0};
short woo[5100][5100] = {0}; //woo[i][j]代表在i前面大於j的有幾個
struct BIT {
int n, a[5100];
void init (int x) {
n = x;
memset(a, 0, sizeof(a));
}
void update (int i, int x) {for (;i <= n; i += i & -i) a[i] += x; }
int query (int i) {
int res = 0;
for (; i > 0; i -= i & -i) res += a[i];
return res;
}
} bit;
int main () {
ios_base::sync_with_stdio(false); cin.tie(0);
memset(dp, 63, sizeof(dp));
dp[0] = 0;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
//順序數對
int cnt = 0;
for (int l = 1; l <= n; l++) {
bit.init(n);
for (int j = 1; j <= n; j++) {
if (a[j] < l) continue;
cost[l][a[j]] = bit.query(a[j]);
bit.update(a[j], 1);
}
for (int r = l; r <= n; r++) cost[l][r + 1] += cost[l][r];
}
//在我前面比r大的跟我換
bit.init(n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n + 2; j++) woo[a[i]][j] = i - 1 - bit.query(j);
bit.update(a[i], 1);
}
for (int r = 1; r <= n; r++) {
int haha = 0;
for (int l = r; l >= 1; l--) {
haha += woo[l][r];
cost[l][r] += haha;
}
}
//dp
for (int i = 1; i <= n; i++) for (int j = 0; j < i; j++) dp[i] = min(dp[i], dp[j] + cost[j + 1][i]);
cout << dp[n] << '\n';
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:30:9: warning: unused variable 'cnt' [-Wunused-variable]
30 | int cnt = 0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2648 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2648 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
2 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4584 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2648 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
2 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4584 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
2 ms |
8540 KB |
Output is correct |
22 |
Correct |
2 ms |
8680 KB |
Output is correct |
23 |
Correct |
2 ms |
8540 KB |
Output is correct |
24 |
Correct |
2 ms |
8540 KB |
Output is correct |
25 |
Correct |
2 ms |
8540 KB |
Output is correct |
26 |
Correct |
2 ms |
8676 KB |
Output is correct |
27 |
Correct |
2 ms |
8540 KB |
Output is correct |
28 |
Correct |
2 ms |
8540 KB |
Output is correct |
29 |
Correct |
2 ms |
8536 KB |
Output is correct |
30 |
Correct |
2 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2648 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
2 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4584 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
2 ms |
8540 KB |
Output is correct |
22 |
Correct |
2 ms |
8680 KB |
Output is correct |
23 |
Correct |
2 ms |
8540 KB |
Output is correct |
24 |
Correct |
2 ms |
8540 KB |
Output is correct |
25 |
Correct |
2 ms |
8540 KB |
Output is correct |
26 |
Correct |
2 ms |
8676 KB |
Output is correct |
27 |
Correct |
2 ms |
8540 KB |
Output is correct |
28 |
Correct |
2 ms |
8540 KB |
Output is correct |
29 |
Correct |
2 ms |
8536 KB |
Output is correct |
30 |
Correct |
2 ms |
8540 KB |
Output is correct |
31 |
Correct |
12 ms |
27228 KB |
Output is correct |
32 |
Correct |
13 ms |
27228 KB |
Output is correct |
33 |
Correct |
10 ms |
27228 KB |
Output is correct |
34 |
Correct |
10 ms |
27228 KB |
Output is correct |
35 |
Correct |
10 ms |
27228 KB |
Output is correct |
36 |
Correct |
10 ms |
27228 KB |
Output is correct |
37 |
Correct |
10 ms |
27152 KB |
Output is correct |
38 |
Correct |
11 ms |
27228 KB |
Output is correct |
39 |
Correct |
10 ms |
27260 KB |
Output is correct |
40 |
Correct |
10 ms |
27228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2648 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
2 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4584 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
2 ms |
8540 KB |
Output is correct |
22 |
Correct |
2 ms |
8680 KB |
Output is correct |
23 |
Correct |
2 ms |
8540 KB |
Output is correct |
24 |
Correct |
2 ms |
8540 KB |
Output is correct |
25 |
Correct |
2 ms |
8540 KB |
Output is correct |
26 |
Correct |
2 ms |
8676 KB |
Output is correct |
27 |
Correct |
2 ms |
8540 KB |
Output is correct |
28 |
Correct |
2 ms |
8540 KB |
Output is correct |
29 |
Correct |
2 ms |
8536 KB |
Output is correct |
30 |
Correct |
2 ms |
8540 KB |
Output is correct |
31 |
Correct |
12 ms |
27228 KB |
Output is correct |
32 |
Correct |
13 ms |
27228 KB |
Output is correct |
33 |
Correct |
10 ms |
27228 KB |
Output is correct |
34 |
Correct |
10 ms |
27228 KB |
Output is correct |
35 |
Correct |
10 ms |
27228 KB |
Output is correct |
36 |
Correct |
10 ms |
27228 KB |
Output is correct |
37 |
Correct |
10 ms |
27152 KB |
Output is correct |
38 |
Correct |
11 ms |
27228 KB |
Output is correct |
39 |
Correct |
10 ms |
27260 KB |
Output is correct |
40 |
Correct |
10 ms |
27228 KB |
Output is correct |
41 |
Correct |
604 ms |
121776 KB |
Output is correct |
42 |
Correct |
616 ms |
121408 KB |
Output is correct |
43 |
Correct |
595 ms |
121648 KB |
Output is correct |
44 |
Correct |
640 ms |
121380 KB |
Output is correct |
45 |
Correct |
606 ms |
121916 KB |
Output is correct |
46 |
Correct |
609 ms |
121652 KB |
Output is correct |
47 |
Correct |
632 ms |
122232 KB |
Output is correct |
48 |
Correct |
642 ms |
119080 KB |
Output is correct |
49 |
Correct |
726 ms |
119084 KB |
Output is correct |
50 |
Correct |
622 ms |
121716 KB |
Output is correct |
51 |
Correct |
613 ms |
120984 KB |
Output is correct |
52 |
Correct |
605 ms |
121168 KB |
Output is correct |
53 |
Correct |
619 ms |
121424 KB |
Output is correct |
54 |
Correct |
619 ms |
120996 KB |
Output is correct |
55 |
Correct |
612 ms |
120996 KB |
Output is correct |