#include <bits/stdc++.h>
using namespace std;
const int MN = 5001;
const int INF = 1e9;
struct BIT {
vector<int> S;
BIT(int n) : S(n) {}
void update(int p, int v) {
while(p < S.size()) {
S[p] += v;
p += p & -p;
}
}
int query(int p) {
int re = 0;
while(p) {
re += S[p];
p -= p & -p;
}
return re;
}
int query(int st, int dr) {
return (st <= 0 ? query(dr) : (query(dr) - query(st - 1)));
}
void clear() {
fill(S.begin(), S.end(), 0);
}
void afis(int n, string mes) {
cout << mes;
for(int i = 1; i <= n; ++i)
cout << query(i, i) << " ";
cout << "\n";
}
};
BIT Activ(MN), Inv(MN), InSol(MN);
int n, A[MN], DP[MN], Rank[MN];
int main() {
cin >> n;
for(int i = 1; i <= n; ++i) {
cin >> A[i];
Rank[A[i]] = i;
}
DP[0] = 0;
for(int i = 1; i <= n; ++i) {
DP[i] = INF;
}
for(int i = 0; i <= n; ++i) {
///adaug asupra solutiei de lungime i
for(int j = 1; j <= n; ++j) {
Activ.update(j, 1);
}
for(int j = 1; j <= i; ++j)
Activ.update(Rank[j], -1); /// luate
int cost = 0;
for(int j = i + 1; j <= n; ++j) {
cost += Activ.query(Rank[j] - 1) - InSol.query(Rank[j], n);
cost += Inv.query(Rank[j] - 1);
InSol.update(Rank[j], 1);
// printf("Din %d in %d\n", i, j);
// Inv.afis(n, "Inv: ");
// Activ.afis(n, "Activ: ");
// InSol.afis(n, "InSol: ");
// printf("costul e %d\n\n", cost);
Inv.update(Rank[j], 1);
Activ.update(Rank[j], -1);
DP[j] = min(DP[j], DP[i] + cost);
}
Activ.clear();
Inv.clear();
InSol.clear();
}
cout << DP[n] << "\n";
return 0;
}
Compilation message
Main.cpp: In member function 'void BIT::update(int, int)':
Main.cpp:12:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
12 | while(p < S.size()) {
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
372 KB |
Output is correct |
2 |
Correct |
1 ms |
368 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
360 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
372 KB |
Output is correct |
2 |
Correct |
1 ms |
368 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
360 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
372 KB |
Output is correct |
2 |
Correct |
1 ms |
368 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
360 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
3 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
3 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
3 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
372 KB |
Output is correct |
2 |
Correct |
1 ms |
368 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
360 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
3 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
3 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
3 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
31 |
Correct |
28 ms |
340 KB |
Output is correct |
32 |
Correct |
20 ms |
340 KB |
Output is correct |
33 |
Correct |
20 ms |
340 KB |
Output is correct |
34 |
Correct |
19 ms |
380 KB |
Output is correct |
35 |
Correct |
19 ms |
376 KB |
Output is correct |
36 |
Correct |
19 ms |
340 KB |
Output is correct |
37 |
Correct |
20 ms |
340 KB |
Output is correct |
38 |
Correct |
19 ms |
340 KB |
Output is correct |
39 |
Correct |
19 ms |
340 KB |
Output is correct |
40 |
Correct |
19 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
372 KB |
Output is correct |
2 |
Correct |
1 ms |
368 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
360 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
3 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
3 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
3 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
31 |
Correct |
28 ms |
340 KB |
Output is correct |
32 |
Correct |
20 ms |
340 KB |
Output is correct |
33 |
Correct |
20 ms |
340 KB |
Output is correct |
34 |
Correct |
19 ms |
380 KB |
Output is correct |
35 |
Correct |
19 ms |
376 KB |
Output is correct |
36 |
Correct |
19 ms |
340 KB |
Output is correct |
37 |
Correct |
20 ms |
340 KB |
Output is correct |
38 |
Correct |
19 ms |
340 KB |
Output is correct |
39 |
Correct |
19 ms |
340 KB |
Output is correct |
40 |
Correct |
19 ms |
384 KB |
Output is correct |
41 |
Correct |
817 ms |
468 KB |
Output is correct |
42 |
Correct |
792 ms |
416 KB |
Output is correct |
43 |
Correct |
797 ms |
420 KB |
Output is correct |
44 |
Correct |
799 ms |
420 KB |
Output is correct |
45 |
Correct |
810 ms |
420 KB |
Output is correct |
46 |
Correct |
799 ms |
420 KB |
Output is correct |
47 |
Correct |
807 ms |
464 KB |
Output is correct |
48 |
Correct |
800 ms |
416 KB |
Output is correct |
49 |
Correct |
792 ms |
416 KB |
Output is correct |
50 |
Correct |
817 ms |
424 KB |
Output is correct |
51 |
Correct |
789 ms |
420 KB |
Output is correct |
52 |
Correct |
785 ms |
424 KB |
Output is correct |
53 |
Correct |
796 ms |
424 KB |
Output is correct |
54 |
Correct |
807 ms |
420 KB |
Output is correct |
55 |
Correct |
789 ms |
340 KB |
Output is correct |