#include <iostream>
#include <vector>
#include <algorithm>
#include <bitset>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <ctime>
#include <queue>
using namespace std;
typedef long long ll;
typedef long double ld;
#define fastInp cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
const ll MOD = 1e9 + 7, MX2 = 1e6, SZ = 5030, INF = 1e9;
ll n;
vector<ll> vec;
ll dp[SZ];
ll invs[SZ][SZ];
signed main() {
fastInp;
cin >> n;
vec.resize(n);
for (auto& c : vec) cin >> c;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (vec[i] > vec[j]) {
invs[vec[j]][vec[i]]++;
}
}
}
for (int i = 1; i <= n; i++) dp[i] = INF;
for (int i = 1; i < SZ; i++) {
for (int l = 0; l < SZ; l++) {
ll r = i + l;
if (r >= SZ) continue;
invs[l][r] += invs[l][r - 1] + invs[l + 1][r] - invs[l + 1][r - 1];
}
}
/* for (int k = 1; k <= n; k++) {
ll l = (k);
ll cst = (invs[0][n] - invs[k + 1][n] - invs[0][k]) + (l * (l - 1) / 2 - invs[0][k]);
dp[k] = min(dp[k], dp[0] + cst);
}*/
for (int i = 1; i <= n; i++) {
ll k = 0, ind = 0;
for (int j = 0; j < n; j++) {
if (vec[j] == i) ind = j;
}
for (int j = 0; j < ind; j++) {
if (vec[j] > i) k++;
}
dp[i] = min(dp[i], dp[i - 1] + k);
for (int k = i + 1; k <= n; k++) {
ll l = (k - i + 1);
ll cst = (invs[i][n] - invs[k + 1][n] - invs[i][k]) + (l * (l - 1) / 2 - invs[i][k]);
dp[k] = min(dp[k], dp[i - 1] + cst);
if (dp[n] == 3) {
cout << "";
}
}
}
cout << dp[n];
return 0;
}
/*
9 12
1 2
4 2
5 8
4 6
6 9
2 9
8 7
8 3
7 1
9 4
3 5
3 4
3 4
2 1
2 3
3 1
1 3
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
254 ms |
118636 KB |
Output is correct |
2 |
Correct |
249 ms |
118636 KB |
Output is correct |
3 |
Correct |
241 ms |
118636 KB |
Output is correct |
4 |
Correct |
250 ms |
118764 KB |
Output is correct |
5 |
Correct |
252 ms |
118636 KB |
Output is correct |
6 |
Correct |
269 ms |
118636 KB |
Output is correct |
7 |
Correct |
248 ms |
118668 KB |
Output is correct |
8 |
Correct |
253 ms |
118636 KB |
Output is correct |
9 |
Correct |
251 ms |
118564 KB |
Output is correct |
10 |
Correct |
245 ms |
118636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
254 ms |
118636 KB |
Output is correct |
2 |
Correct |
249 ms |
118636 KB |
Output is correct |
3 |
Correct |
241 ms |
118636 KB |
Output is correct |
4 |
Correct |
250 ms |
118764 KB |
Output is correct |
5 |
Correct |
252 ms |
118636 KB |
Output is correct |
6 |
Correct |
269 ms |
118636 KB |
Output is correct |
7 |
Correct |
248 ms |
118668 KB |
Output is correct |
8 |
Correct |
253 ms |
118636 KB |
Output is correct |
9 |
Correct |
251 ms |
118564 KB |
Output is correct |
10 |
Correct |
245 ms |
118636 KB |
Output is correct |
11 |
Correct |
267 ms |
118764 KB |
Output is correct |
12 |
Correct |
246 ms |
118636 KB |
Output is correct |
13 |
Correct |
246 ms |
118644 KB |
Output is correct |
14 |
Correct |
261 ms |
118636 KB |
Output is correct |
15 |
Correct |
244 ms |
118636 KB |
Output is correct |
16 |
Correct |
249 ms |
118764 KB |
Output is correct |
17 |
Correct |
247 ms |
118616 KB |
Output is correct |
18 |
Correct |
243 ms |
118636 KB |
Output is correct |
19 |
Correct |
246 ms |
118648 KB |
Output is correct |
20 |
Correct |
289 ms |
118636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
254 ms |
118636 KB |
Output is correct |
2 |
Correct |
249 ms |
118636 KB |
Output is correct |
3 |
Correct |
241 ms |
118636 KB |
Output is correct |
4 |
Correct |
250 ms |
118764 KB |
Output is correct |
5 |
Correct |
252 ms |
118636 KB |
Output is correct |
6 |
Correct |
269 ms |
118636 KB |
Output is correct |
7 |
Correct |
248 ms |
118668 KB |
Output is correct |
8 |
Correct |
253 ms |
118636 KB |
Output is correct |
9 |
Correct |
251 ms |
118564 KB |
Output is correct |
10 |
Correct |
245 ms |
118636 KB |
Output is correct |
11 |
Correct |
267 ms |
118764 KB |
Output is correct |
12 |
Correct |
246 ms |
118636 KB |
Output is correct |
13 |
Correct |
246 ms |
118644 KB |
Output is correct |
14 |
Correct |
261 ms |
118636 KB |
Output is correct |
15 |
Correct |
244 ms |
118636 KB |
Output is correct |
16 |
Correct |
249 ms |
118764 KB |
Output is correct |
17 |
Correct |
247 ms |
118616 KB |
Output is correct |
18 |
Correct |
243 ms |
118636 KB |
Output is correct |
19 |
Correct |
246 ms |
118648 KB |
Output is correct |
20 |
Correct |
289 ms |
118636 KB |
Output is correct |
21 |
Correct |
251 ms |
118636 KB |
Output is correct |
22 |
Correct |
247 ms |
118636 KB |
Output is correct |
23 |
Correct |
245 ms |
118636 KB |
Output is correct |
24 |
Correct |
246 ms |
118636 KB |
Output is correct |
25 |
Correct |
245 ms |
118636 KB |
Output is correct |
26 |
Correct |
253 ms |
118636 KB |
Output is correct |
27 |
Correct |
243 ms |
118636 KB |
Output is correct |
28 |
Correct |
251 ms |
118636 KB |
Output is correct |
29 |
Correct |
262 ms |
118508 KB |
Output is correct |
30 |
Correct |
246 ms |
118636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
254 ms |
118636 KB |
Output is correct |
2 |
Correct |
249 ms |
118636 KB |
Output is correct |
3 |
Correct |
241 ms |
118636 KB |
Output is correct |
4 |
Correct |
250 ms |
118764 KB |
Output is correct |
5 |
Correct |
252 ms |
118636 KB |
Output is correct |
6 |
Correct |
269 ms |
118636 KB |
Output is correct |
7 |
Correct |
248 ms |
118668 KB |
Output is correct |
8 |
Correct |
253 ms |
118636 KB |
Output is correct |
9 |
Correct |
251 ms |
118564 KB |
Output is correct |
10 |
Correct |
245 ms |
118636 KB |
Output is correct |
11 |
Correct |
267 ms |
118764 KB |
Output is correct |
12 |
Correct |
246 ms |
118636 KB |
Output is correct |
13 |
Correct |
246 ms |
118644 KB |
Output is correct |
14 |
Correct |
261 ms |
118636 KB |
Output is correct |
15 |
Correct |
244 ms |
118636 KB |
Output is correct |
16 |
Correct |
249 ms |
118764 KB |
Output is correct |
17 |
Correct |
247 ms |
118616 KB |
Output is correct |
18 |
Correct |
243 ms |
118636 KB |
Output is correct |
19 |
Correct |
246 ms |
118648 KB |
Output is correct |
20 |
Correct |
289 ms |
118636 KB |
Output is correct |
21 |
Correct |
251 ms |
118636 KB |
Output is correct |
22 |
Correct |
247 ms |
118636 KB |
Output is correct |
23 |
Correct |
245 ms |
118636 KB |
Output is correct |
24 |
Correct |
246 ms |
118636 KB |
Output is correct |
25 |
Correct |
245 ms |
118636 KB |
Output is correct |
26 |
Correct |
253 ms |
118636 KB |
Output is correct |
27 |
Correct |
243 ms |
118636 KB |
Output is correct |
28 |
Correct |
251 ms |
118636 KB |
Output is correct |
29 |
Correct |
262 ms |
118508 KB |
Output is correct |
30 |
Correct |
246 ms |
118636 KB |
Output is correct |
31 |
Correct |
256 ms |
118548 KB |
Output is correct |
32 |
Correct |
286 ms |
118636 KB |
Output is correct |
33 |
Correct |
247 ms |
118636 KB |
Output is correct |
34 |
Correct |
252 ms |
118764 KB |
Output is correct |
35 |
Correct |
256 ms |
118636 KB |
Output is correct |
36 |
Correct |
250 ms |
118636 KB |
Output is correct |
37 |
Correct |
254 ms |
118636 KB |
Output is correct |
38 |
Correct |
249 ms |
118636 KB |
Output is correct |
39 |
Correct |
245 ms |
118636 KB |
Output is correct |
40 |
Correct |
253 ms |
118764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
254 ms |
118636 KB |
Output is correct |
2 |
Correct |
249 ms |
118636 KB |
Output is correct |
3 |
Correct |
241 ms |
118636 KB |
Output is correct |
4 |
Correct |
250 ms |
118764 KB |
Output is correct |
5 |
Correct |
252 ms |
118636 KB |
Output is correct |
6 |
Correct |
269 ms |
118636 KB |
Output is correct |
7 |
Correct |
248 ms |
118668 KB |
Output is correct |
8 |
Correct |
253 ms |
118636 KB |
Output is correct |
9 |
Correct |
251 ms |
118564 KB |
Output is correct |
10 |
Correct |
245 ms |
118636 KB |
Output is correct |
11 |
Correct |
267 ms |
118764 KB |
Output is correct |
12 |
Correct |
246 ms |
118636 KB |
Output is correct |
13 |
Correct |
246 ms |
118644 KB |
Output is correct |
14 |
Correct |
261 ms |
118636 KB |
Output is correct |
15 |
Correct |
244 ms |
118636 KB |
Output is correct |
16 |
Correct |
249 ms |
118764 KB |
Output is correct |
17 |
Correct |
247 ms |
118616 KB |
Output is correct |
18 |
Correct |
243 ms |
118636 KB |
Output is correct |
19 |
Correct |
246 ms |
118648 KB |
Output is correct |
20 |
Correct |
289 ms |
118636 KB |
Output is correct |
21 |
Correct |
251 ms |
118636 KB |
Output is correct |
22 |
Correct |
247 ms |
118636 KB |
Output is correct |
23 |
Correct |
245 ms |
118636 KB |
Output is correct |
24 |
Correct |
246 ms |
118636 KB |
Output is correct |
25 |
Correct |
245 ms |
118636 KB |
Output is correct |
26 |
Correct |
253 ms |
118636 KB |
Output is correct |
27 |
Correct |
243 ms |
118636 KB |
Output is correct |
28 |
Correct |
251 ms |
118636 KB |
Output is correct |
29 |
Correct |
262 ms |
118508 KB |
Output is correct |
30 |
Correct |
246 ms |
118636 KB |
Output is correct |
31 |
Correct |
256 ms |
118548 KB |
Output is correct |
32 |
Correct |
286 ms |
118636 KB |
Output is correct |
33 |
Correct |
247 ms |
118636 KB |
Output is correct |
34 |
Correct |
252 ms |
118764 KB |
Output is correct |
35 |
Correct |
256 ms |
118636 KB |
Output is correct |
36 |
Correct |
250 ms |
118636 KB |
Output is correct |
37 |
Correct |
254 ms |
118636 KB |
Output is correct |
38 |
Correct |
249 ms |
118636 KB |
Output is correct |
39 |
Correct |
245 ms |
118636 KB |
Output is correct |
40 |
Correct |
253 ms |
118764 KB |
Output is correct |
41 |
Correct |
602 ms |
118892 KB |
Output is correct |
42 |
Correct |
599 ms |
118736 KB |
Output is correct |
43 |
Correct |
610 ms |
118684 KB |
Output is correct |
44 |
Correct |
635 ms |
118764 KB |
Output is correct |
45 |
Correct |
593 ms |
118636 KB |
Output is correct |
46 |
Correct |
608 ms |
118764 KB |
Output is correct |
47 |
Correct |
610 ms |
118636 KB |
Output is correct |
48 |
Correct |
616 ms |
118764 KB |
Output is correct |
49 |
Correct |
600 ms |
118892 KB |
Output is correct |
50 |
Correct |
618 ms |
118764 KB |
Output is correct |
51 |
Correct |
595 ms |
118636 KB |
Output is correct |
52 |
Correct |
619 ms |
118664 KB |
Output is correct |
53 |
Correct |
620 ms |
118784 KB |
Output is correct |
54 |
Correct |
595 ms |
118764 KB |
Output is correct |
55 |
Correct |
596 ms |
118784 KB |
Output is correct |