#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mxN = 5007;
int arr[mxN], pos[mxN], inv[mxN][mxN], dp[mxN];
int f(int h) {
if (!h) {
return 0;
}
if (dp[h] != -1) {
return dp[h];
}
int ret = INT_MAX, add = 0;
for (int x = h; x >= 1; --x) {
add += h - pos[x] + 2*inv[x][h] - inv[x][x];
ret = min(ret, f(x-1) + add);
}
return dp[h] = ret;
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> arr[i];
pos[arr[i]] = i;
}
for (int y = 1; y <= n; ++y) {
int cnt = 0;
for (int i = 1; i <= n; ++i) {
if (arr[i] > y) {
++cnt;
}
inv[arr[i]][y] = cnt;
}
}
memset(dp, -1, sizeof(dp));
cout << f(n) << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
21 |
Correct |
2 ms |
8536 KB |
Output is correct |
22 |
Correct |
1 ms |
8540 KB |
Output is correct |
23 |
Correct |
1 ms |
8652 KB |
Output is correct |
24 |
Correct |
1 ms |
8540 KB |
Output is correct |
25 |
Correct |
2 ms |
8540 KB |
Output is correct |
26 |
Correct |
1 ms |
8660 KB |
Output is correct |
27 |
Correct |
1 ms |
8540 KB |
Output is correct |
28 |
Correct |
1 ms |
8792 KB |
Output is correct |
29 |
Correct |
2 ms |
8540 KB |
Output is correct |
30 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
21 |
Correct |
2 ms |
8536 KB |
Output is correct |
22 |
Correct |
1 ms |
8540 KB |
Output is correct |
23 |
Correct |
1 ms |
8652 KB |
Output is correct |
24 |
Correct |
1 ms |
8540 KB |
Output is correct |
25 |
Correct |
2 ms |
8540 KB |
Output is correct |
26 |
Correct |
1 ms |
8660 KB |
Output is correct |
27 |
Correct |
1 ms |
8540 KB |
Output is correct |
28 |
Correct |
1 ms |
8792 KB |
Output is correct |
29 |
Correct |
2 ms |
8540 KB |
Output is correct |
30 |
Correct |
1 ms |
8540 KB |
Output is correct |
31 |
Correct |
9 ms |
33380 KB |
Output is correct |
32 |
Correct |
6 ms |
33372 KB |
Output is correct |
33 |
Correct |
6 ms |
33372 KB |
Output is correct |
34 |
Correct |
6 ms |
33368 KB |
Output is correct |
35 |
Correct |
6 ms |
33372 KB |
Output is correct |
36 |
Correct |
6 ms |
33272 KB |
Output is correct |
37 |
Correct |
6 ms |
33260 KB |
Output is correct |
38 |
Correct |
6 ms |
33372 KB |
Output is correct |
39 |
Correct |
7 ms |
33372 KB |
Output is correct |
40 |
Correct |
6 ms |
33372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
21 |
Correct |
2 ms |
8536 KB |
Output is correct |
22 |
Correct |
1 ms |
8540 KB |
Output is correct |
23 |
Correct |
1 ms |
8652 KB |
Output is correct |
24 |
Correct |
1 ms |
8540 KB |
Output is correct |
25 |
Correct |
2 ms |
8540 KB |
Output is correct |
26 |
Correct |
1 ms |
8660 KB |
Output is correct |
27 |
Correct |
1 ms |
8540 KB |
Output is correct |
28 |
Correct |
1 ms |
8792 KB |
Output is correct |
29 |
Correct |
2 ms |
8540 KB |
Output is correct |
30 |
Correct |
1 ms |
8540 KB |
Output is correct |
31 |
Correct |
9 ms |
33380 KB |
Output is correct |
32 |
Correct |
6 ms |
33372 KB |
Output is correct |
33 |
Correct |
6 ms |
33372 KB |
Output is correct |
34 |
Correct |
6 ms |
33368 KB |
Output is correct |
35 |
Correct |
6 ms |
33372 KB |
Output is correct |
36 |
Correct |
6 ms |
33272 KB |
Output is correct |
37 |
Correct |
6 ms |
33260 KB |
Output is correct |
38 |
Correct |
6 ms |
33372 KB |
Output is correct |
39 |
Correct |
7 ms |
33372 KB |
Output is correct |
40 |
Correct |
6 ms |
33372 KB |
Output is correct |
41 |
Correct |
254 ms |
196944 KB |
Output is correct |
42 |
Correct |
249 ms |
196688 KB |
Output is correct |
43 |
Correct |
237 ms |
196664 KB |
Output is correct |
44 |
Correct |
227 ms |
196804 KB |
Output is correct |
45 |
Correct |
226 ms |
196688 KB |
Output is correct |
46 |
Correct |
231 ms |
196948 KB |
Output is correct |
47 |
Correct |
231 ms |
196944 KB |
Output is correct |
48 |
Correct |
226 ms |
196780 KB |
Output is correct |
49 |
Correct |
231 ms |
197008 KB |
Output is correct |
50 |
Correct |
228 ms |
196692 KB |
Output is correct |
51 |
Correct |
223 ms |
196940 KB |
Output is correct |
52 |
Correct |
235 ms |
196916 KB |
Output is correct |
53 |
Correct |
226 ms |
196764 KB |
Output is correct |
54 |
Correct |
240 ms |
196940 KB |
Output is correct |
55 |
Correct |
229 ms |
196728 KB |
Output is correct |