#include <bits/stdc++.h>
using namespace std;
int main() {
int N; cin >> N;
vector<int> H(N);
vector<int> P(N);
for(int i = 0; i < N; i++) {
cin >> H[i];
--H[i];
P[H[i]] = i;
}
vector<int> dp(N + 1);
vector<vector<int>> l(N, vector<int>(N));
for(int j = 0; j < N; j++) {
for(int i = 0; i < j; i++) {
if(i > 0) l[i][j] = l[i - 1][j];
l[i][j] += (P[i] > P[j]);
}
}
vector<vector<int>> inv(N, vector<int>(N));
for(int len = 2; len <= N; len++) {
for(int i = 0; i + len - 1 < N; i++) {
int j = i + len - 1;
inv[i][j] = inv[i][j - 1] + inv[i + 1][j] - inv[i + 1][j - 1] + (P[i] < P[j]);
}
}
for(int i = N - 1; i >= 0; i--) {
dp[i] = 1000000000;
int sum = 0;
for(int j = i; j < N; j++) {
sum += P[j];
if(i > 0) sum += l[i - 1][j];
int cost = sum + inv[i][j] - (i + j) * (j - i + 1) / 2;
dp[i] = min(dp[i], dp[j + 1] + cost);
}
}
cout << dp[0];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
292 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
292 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
292 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
292 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
292 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
292 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
292 KB |
Output is correct |
21 |
Correct |
1 ms |
588 KB |
Output is correct |
22 |
Correct |
1 ms |
536 KB |
Output is correct |
23 |
Correct |
1 ms |
588 KB |
Output is correct |
24 |
Correct |
1 ms |
588 KB |
Output is correct |
25 |
Correct |
1 ms |
580 KB |
Output is correct |
26 |
Correct |
1 ms |
588 KB |
Output is correct |
27 |
Correct |
1 ms |
548 KB |
Output is correct |
28 |
Correct |
1 ms |
544 KB |
Output is correct |
29 |
Correct |
1 ms |
588 KB |
Output is correct |
30 |
Correct |
1 ms |
588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
292 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
292 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
292 KB |
Output is correct |
21 |
Correct |
1 ms |
588 KB |
Output is correct |
22 |
Correct |
1 ms |
536 KB |
Output is correct |
23 |
Correct |
1 ms |
588 KB |
Output is correct |
24 |
Correct |
1 ms |
588 KB |
Output is correct |
25 |
Correct |
1 ms |
580 KB |
Output is correct |
26 |
Correct |
1 ms |
588 KB |
Output is correct |
27 |
Correct |
1 ms |
548 KB |
Output is correct |
28 |
Correct |
1 ms |
544 KB |
Output is correct |
29 |
Correct |
1 ms |
588 KB |
Output is correct |
30 |
Correct |
1 ms |
588 KB |
Output is correct |
31 |
Correct |
6 ms |
5288 KB |
Output is correct |
32 |
Correct |
6 ms |
5324 KB |
Output is correct |
33 |
Correct |
7 ms |
5324 KB |
Output is correct |
34 |
Correct |
7 ms |
5324 KB |
Output is correct |
35 |
Correct |
7 ms |
5268 KB |
Output is correct |
36 |
Correct |
6 ms |
5324 KB |
Output is correct |
37 |
Correct |
8 ms |
5264 KB |
Output is correct |
38 |
Correct |
6 ms |
5324 KB |
Output is correct |
39 |
Correct |
6 ms |
5324 KB |
Output is correct |
40 |
Correct |
7 ms |
5324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
292 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
292 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
292 KB |
Output is correct |
21 |
Correct |
1 ms |
588 KB |
Output is correct |
22 |
Correct |
1 ms |
536 KB |
Output is correct |
23 |
Correct |
1 ms |
588 KB |
Output is correct |
24 |
Correct |
1 ms |
588 KB |
Output is correct |
25 |
Correct |
1 ms |
580 KB |
Output is correct |
26 |
Correct |
1 ms |
588 KB |
Output is correct |
27 |
Correct |
1 ms |
548 KB |
Output is correct |
28 |
Correct |
1 ms |
544 KB |
Output is correct |
29 |
Correct |
1 ms |
588 KB |
Output is correct |
30 |
Correct |
1 ms |
588 KB |
Output is correct |
31 |
Correct |
6 ms |
5288 KB |
Output is correct |
32 |
Correct |
6 ms |
5324 KB |
Output is correct |
33 |
Correct |
7 ms |
5324 KB |
Output is correct |
34 |
Correct |
7 ms |
5324 KB |
Output is correct |
35 |
Correct |
7 ms |
5268 KB |
Output is correct |
36 |
Correct |
6 ms |
5324 KB |
Output is correct |
37 |
Correct |
8 ms |
5264 KB |
Output is correct |
38 |
Correct |
6 ms |
5324 KB |
Output is correct |
39 |
Correct |
6 ms |
5324 KB |
Output is correct |
40 |
Correct |
7 ms |
5324 KB |
Output is correct |
41 |
Correct |
357 ms |
196200 KB |
Output is correct |
42 |
Correct |
357 ms |
196220 KB |
Output is correct |
43 |
Correct |
342 ms |
196424 KB |
Output is correct |
44 |
Correct |
349 ms |
196420 KB |
Output is correct |
45 |
Correct |
349 ms |
196444 KB |
Output is correct |
46 |
Correct |
355 ms |
196420 KB |
Output is correct |
47 |
Correct |
377 ms |
196448 KB |
Output is correct |
48 |
Correct |
353 ms |
196336 KB |
Output is correct |
49 |
Correct |
374 ms |
196528 KB |
Output is correct |
50 |
Correct |
362 ms |
196420 KB |
Output is correct |
51 |
Correct |
384 ms |
196364 KB |
Output is correct |
52 |
Correct |
354 ms |
196424 KB |
Output is correct |
53 |
Correct |
359 ms |
196396 KB |
Output is correct |
54 |
Correct |
363 ms |
196420 KB |
Output is correct |
55 |
Correct |
365 ms |
196468 KB |
Output is correct |