#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int n, pos[5001], dp[5001], bit1[5001], bit2[5001];
void update(int *bit, int pos, int val) {
for (; pos <= n; pos += pos & -pos) bit[pos] += val;
}
int query(int *bit, int a, int b) {
int ans = 0;
for (; b; b -= b & -b) ans += bit[b];
for (a--; a; a -= a & -a) ans -= bit[a];
return ans;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; i++) {
int a;
cin >> a;
pos[a] = i;
}
for (int i = 1; i <= n; i++) {
dp[i] = INT_MAX;
// First, get inversions from [1; i]
int inversions = 0;
for (int j = 1; j <= i; j++) {
inversions += query(bit1, 1, pos[j] - 1);
update(bit1, pos[j], 1);
}
// Then find the best j for dp[i]
memset(bit2, 0, sizeof bit2);
for (int j = 0; j < i; j++) {
dp[i] = min(dp[i], dp[j] + inversions);
// First, remove j + 1 from bit1 and subtract its contribution
inversions -= query(bit1, pos[j + 1] + 1, n) + query(bit2, pos[j + 1] + 1, n);
update(bit1, pos[j + 1], -1);
// Next, add j + 1 to bit2
inversions += query(bit1, 1, pos[j + 1] - 1);
update(bit2, pos[j + 1], 1);
}
}
cout << dp[n];
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
496 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
496 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
496 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
2 ms |
364 KB |
Output is correct |
23 |
Correct |
2 ms |
364 KB |
Output is correct |
24 |
Correct |
2 ms |
364 KB |
Output is correct |
25 |
Correct |
6 ms |
364 KB |
Output is correct |
26 |
Correct |
2 ms |
364 KB |
Output is correct |
27 |
Correct |
2 ms |
364 KB |
Output is correct |
28 |
Correct |
2 ms |
364 KB |
Output is correct |
29 |
Correct |
2 ms |
504 KB |
Output is correct |
30 |
Correct |
2 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
496 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
2 ms |
364 KB |
Output is correct |
23 |
Correct |
2 ms |
364 KB |
Output is correct |
24 |
Correct |
2 ms |
364 KB |
Output is correct |
25 |
Correct |
6 ms |
364 KB |
Output is correct |
26 |
Correct |
2 ms |
364 KB |
Output is correct |
27 |
Correct |
2 ms |
364 KB |
Output is correct |
28 |
Correct |
2 ms |
364 KB |
Output is correct |
29 |
Correct |
2 ms |
504 KB |
Output is correct |
30 |
Correct |
2 ms |
364 KB |
Output is correct |
31 |
Correct |
21 ms |
364 KB |
Output is correct |
32 |
Correct |
21 ms |
364 KB |
Output is correct |
33 |
Correct |
21 ms |
364 KB |
Output is correct |
34 |
Correct |
21 ms |
364 KB |
Output is correct |
35 |
Correct |
19 ms |
364 KB |
Output is correct |
36 |
Correct |
19 ms |
364 KB |
Output is correct |
37 |
Correct |
25 ms |
364 KB |
Output is correct |
38 |
Correct |
25 ms |
364 KB |
Output is correct |
39 |
Correct |
24 ms |
364 KB |
Output is correct |
40 |
Correct |
27 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
496 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
2 ms |
364 KB |
Output is correct |
23 |
Correct |
2 ms |
364 KB |
Output is correct |
24 |
Correct |
2 ms |
364 KB |
Output is correct |
25 |
Correct |
6 ms |
364 KB |
Output is correct |
26 |
Correct |
2 ms |
364 KB |
Output is correct |
27 |
Correct |
2 ms |
364 KB |
Output is correct |
28 |
Correct |
2 ms |
364 KB |
Output is correct |
29 |
Correct |
2 ms |
504 KB |
Output is correct |
30 |
Correct |
2 ms |
364 KB |
Output is correct |
31 |
Correct |
21 ms |
364 KB |
Output is correct |
32 |
Correct |
21 ms |
364 KB |
Output is correct |
33 |
Correct |
21 ms |
364 KB |
Output is correct |
34 |
Correct |
21 ms |
364 KB |
Output is correct |
35 |
Correct |
19 ms |
364 KB |
Output is correct |
36 |
Correct |
19 ms |
364 KB |
Output is correct |
37 |
Correct |
25 ms |
364 KB |
Output is correct |
38 |
Correct |
25 ms |
364 KB |
Output is correct |
39 |
Correct |
24 ms |
364 KB |
Output is correct |
40 |
Correct |
27 ms |
364 KB |
Output is correct |
41 |
Correct |
1043 ms |
496 KB |
Output is correct |
42 |
Correct |
1035 ms |
632 KB |
Output is correct |
43 |
Correct |
1034 ms |
492 KB |
Output is correct |
44 |
Correct |
1051 ms |
620 KB |
Output is correct |
45 |
Correct |
1100 ms |
364 KB |
Output is correct |
46 |
Correct |
1004 ms |
492 KB |
Output is correct |
47 |
Correct |
1091 ms |
492 KB |
Output is correct |
48 |
Correct |
991 ms |
364 KB |
Output is correct |
49 |
Correct |
1099 ms |
620 KB |
Output is correct |
50 |
Correct |
1066 ms |
752 KB |
Output is correct |
51 |
Correct |
1006 ms |
716 KB |
Output is correct |
52 |
Correct |
1072 ms |
748 KB |
Output is correct |
53 |
Correct |
1026 ms |
620 KB |
Output is correct |
54 |
Correct |
1041 ms |
640 KB |
Output is correct |
55 |
Correct |
1030 ms |
592 KB |
Output is correct |