//
// Created by 42kangaroo on 28/08/2023.
//
#include "bits/stdc++.h"
using namespace std;
#define int long long
vector<vector<int>> weightF(vector<int>& a) {
// get inversion direction
vector<vector<bool>> dI(a.size() + 1, vector<bool>(a.size() + 1));
for (int i = 0; i < a.size(); ++i) {
for (int j = 0; j < i; ++j) {
if (a[i] > a[j]) {
dI[a[i]][a[j]] = true; // second is greater
} else {
dI[a[j]][a[i]] = false;
}
}
}
// add to new as dif array
vector<vector<int>> cD(a.size() + 1, vector<int>(a.size() + 1, 0));
for (int i = 1; i < cD.size(); ++i) {
int le = 0;
for (int j = 1; j < i; ++j) {
cD[i][j] = (dI[i][j] ? 1 : -1);
le += !dI[i][j];
}
cD[i][i] = le;
}
// calc dif array
vector<vector<int>> c(a.size() + 1, vector<int>(a.size() + 1, 0));
for (int i = 1; i < c.size(); ++i) {
for (int j = i; j >= 0; --j) {
if (j != c.size() - 1) c[i][j] += cD[i][j + 1] + c[i][j + 1];
}
for (int j = i; j >= 0; --j) {
c[i][j] += c[i - 1][j];
}
}
return c;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
vector<int> dp(n + 1, 1e9);
dp[0] = 0;
vector<vector<int>> c = weightF(a);
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < i; ++j) {
dp[i] = min(dp[i], dp[j] + c[i][j]);
}
}
cout << dp[n];
}
Compilation message
Main.cpp: In function 'std::vector<std::vector<long long int> > weightF(std::vector<long long int>&)':
Main.cpp:13:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
13 | for (int i = 0; i < a.size(); ++i) {
| ~~^~~~~~~~~~
Main.cpp:24:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for (int i = 1; i < cD.size(); ++i) {
| ~~^~~~~~~~~~~
Main.cpp:34:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for (int i = 1; i < c.size(); ++i) {
| ~~^~~~~~~~~~
Main.cpp:36:10: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | if (j != c.size() - 1) c[i][j] += cD[i][j + 1] + c[i][j + 1];
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
852 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
1 ms |
852 KB |
Output is correct |
24 |
Correct |
1 ms |
980 KB |
Output is correct |
25 |
Correct |
1 ms |
852 KB |
Output is correct |
26 |
Correct |
1 ms |
980 KB |
Output is correct |
27 |
Correct |
1 ms |
980 KB |
Output is correct |
28 |
Correct |
1 ms |
980 KB |
Output is correct |
29 |
Correct |
1 ms |
980 KB |
Output is correct |
30 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
852 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
1 ms |
852 KB |
Output is correct |
24 |
Correct |
1 ms |
980 KB |
Output is correct |
25 |
Correct |
1 ms |
852 KB |
Output is correct |
26 |
Correct |
1 ms |
980 KB |
Output is correct |
27 |
Correct |
1 ms |
980 KB |
Output is correct |
28 |
Correct |
1 ms |
980 KB |
Output is correct |
29 |
Correct |
1 ms |
980 KB |
Output is correct |
30 |
Correct |
1 ms |
980 KB |
Output is correct |
31 |
Correct |
9 ms |
10452 KB |
Output is correct |
32 |
Correct |
7 ms |
10452 KB |
Output is correct |
33 |
Correct |
7 ms |
10452 KB |
Output is correct |
34 |
Correct |
7 ms |
10452 KB |
Output is correct |
35 |
Correct |
7 ms |
10452 KB |
Output is correct |
36 |
Correct |
7 ms |
10452 KB |
Output is correct |
37 |
Correct |
7 ms |
10452 KB |
Output is correct |
38 |
Correct |
7 ms |
10452 KB |
Output is correct |
39 |
Correct |
7 ms |
10452 KB |
Output is correct |
40 |
Correct |
8 ms |
10452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
852 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
1 ms |
852 KB |
Output is correct |
24 |
Correct |
1 ms |
980 KB |
Output is correct |
25 |
Correct |
1 ms |
852 KB |
Output is correct |
26 |
Correct |
1 ms |
980 KB |
Output is correct |
27 |
Correct |
1 ms |
980 KB |
Output is correct |
28 |
Correct |
1 ms |
980 KB |
Output is correct |
29 |
Correct |
1 ms |
980 KB |
Output is correct |
30 |
Correct |
1 ms |
980 KB |
Output is correct |
31 |
Correct |
9 ms |
10452 KB |
Output is correct |
32 |
Correct |
7 ms |
10452 KB |
Output is correct |
33 |
Correct |
7 ms |
10452 KB |
Output is correct |
34 |
Correct |
7 ms |
10452 KB |
Output is correct |
35 |
Correct |
7 ms |
10452 KB |
Output is correct |
36 |
Correct |
7 ms |
10452 KB |
Output is correct |
37 |
Correct |
7 ms |
10452 KB |
Output is correct |
38 |
Correct |
7 ms |
10452 KB |
Output is correct |
39 |
Correct |
7 ms |
10452 KB |
Output is correct |
40 |
Correct |
8 ms |
10452 KB |
Output is correct |
41 |
Correct |
256 ms |
395244 KB |
Output is correct |
42 |
Correct |
252 ms |
395196 KB |
Output is correct |
43 |
Correct |
237 ms |
395468 KB |
Output is correct |
44 |
Correct |
253 ms |
395436 KB |
Output is correct |
45 |
Correct |
240 ms |
395468 KB |
Output is correct |
46 |
Correct |
247 ms |
395560 KB |
Output is correct |
47 |
Correct |
247 ms |
395580 KB |
Output is correct |
48 |
Correct |
238 ms |
395560 KB |
Output is correct |
49 |
Correct |
245 ms |
395512 KB |
Output is correct |
50 |
Correct |
244 ms |
395620 KB |
Output is correct |
51 |
Correct |
239 ms |
395512 KB |
Output is correct |
52 |
Correct |
238 ms |
395548 KB |
Output is correct |
53 |
Correct |
245 ms |
395532 KB |
Output is correct |
54 |
Correct |
248 ms |
395620 KB |
Output is correct |
55 |
Correct |
257 ms |
395596 KB |
Output is correct |