#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 5e3 + 2;
const int inf = 2e9 + 2;
int dp[N][N], pref[N][N], p[N], ip[N], mn[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n; cin >> n;
for (int i = 1; i <= n; i++) {
cin >> p[i];
ip[p[i]] = i;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j < i; j++) {
if (ip[j] > ip[i]) pref[i][j] = 1;
pref[i][j] += pref[i][j - 1];
}
}
for (int i = 1; i <= n; i++) {
mn[i] = inf;
for (int j = 1; j <= i; j++) dp[i][j] = inf;
}
for (int i = 1; i <= n; i++) {
dp[i][1] = mn[i] = mn[i - 1] + pref[i][i - 1];
for (int j = 2; j <= i; j++) {
dp[i][j] = dp[i - 1][j - 1] + 2 * pref[i][i - j] - pref[i][i - 1] + j - 1;
smin(mn[i], dp[i][j]);
}
}
cout << mn[n] << en;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 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 |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 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 |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
456 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
500 KB |
Output is correct |
17 |
Correct |
1 ms |
448 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
456 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 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 |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
456 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
500 KB |
Output is correct |
17 |
Correct |
1 ms |
448 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
456 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
2004 KB |
Output is correct |
22 |
Correct |
2 ms |
2004 KB |
Output is correct |
23 |
Correct |
1 ms |
2004 KB |
Output is correct |
24 |
Correct |
2 ms |
2124 KB |
Output is correct |
25 |
Correct |
1 ms |
2116 KB |
Output is correct |
26 |
Correct |
1 ms |
2004 KB |
Output is correct |
27 |
Correct |
1 ms |
2004 KB |
Output is correct |
28 |
Correct |
1 ms |
2004 KB |
Output is correct |
29 |
Correct |
1 ms |
2004 KB |
Output is correct |
30 |
Correct |
1 ms |
2004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 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 |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
456 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
500 KB |
Output is correct |
17 |
Correct |
1 ms |
448 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
456 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
2004 KB |
Output is correct |
22 |
Correct |
2 ms |
2004 KB |
Output is correct |
23 |
Correct |
1 ms |
2004 KB |
Output is correct |
24 |
Correct |
2 ms |
2124 KB |
Output is correct |
25 |
Correct |
1 ms |
2116 KB |
Output is correct |
26 |
Correct |
1 ms |
2004 KB |
Output is correct |
27 |
Correct |
1 ms |
2004 KB |
Output is correct |
28 |
Correct |
1 ms |
2004 KB |
Output is correct |
29 |
Correct |
1 ms |
2004 KB |
Output is correct |
30 |
Correct |
1 ms |
2004 KB |
Output is correct |
31 |
Correct |
6 ms |
9172 KB |
Output is correct |
32 |
Correct |
6 ms |
9300 KB |
Output is correct |
33 |
Correct |
6 ms |
9172 KB |
Output is correct |
34 |
Correct |
6 ms |
9300 KB |
Output is correct |
35 |
Correct |
6 ms |
9292 KB |
Output is correct |
36 |
Correct |
6 ms |
9288 KB |
Output is correct |
37 |
Correct |
6 ms |
9300 KB |
Output is correct |
38 |
Correct |
6 ms |
9300 KB |
Output is correct |
39 |
Correct |
7 ms |
9248 KB |
Output is correct |
40 |
Correct |
6 ms |
9288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 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 |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
456 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
500 KB |
Output is correct |
17 |
Correct |
1 ms |
448 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
456 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
2004 KB |
Output is correct |
22 |
Correct |
2 ms |
2004 KB |
Output is correct |
23 |
Correct |
1 ms |
2004 KB |
Output is correct |
24 |
Correct |
2 ms |
2124 KB |
Output is correct |
25 |
Correct |
1 ms |
2116 KB |
Output is correct |
26 |
Correct |
1 ms |
2004 KB |
Output is correct |
27 |
Correct |
1 ms |
2004 KB |
Output is correct |
28 |
Correct |
1 ms |
2004 KB |
Output is correct |
29 |
Correct |
1 ms |
2004 KB |
Output is correct |
30 |
Correct |
1 ms |
2004 KB |
Output is correct |
31 |
Correct |
6 ms |
9172 KB |
Output is correct |
32 |
Correct |
6 ms |
9300 KB |
Output is correct |
33 |
Correct |
6 ms |
9172 KB |
Output is correct |
34 |
Correct |
6 ms |
9300 KB |
Output is correct |
35 |
Correct |
6 ms |
9292 KB |
Output is correct |
36 |
Correct |
6 ms |
9288 KB |
Output is correct |
37 |
Correct |
6 ms |
9300 KB |
Output is correct |
38 |
Correct |
6 ms |
9300 KB |
Output is correct |
39 |
Correct |
7 ms |
9248 KB |
Output is correct |
40 |
Correct |
6 ms |
9288 KB |
Output is correct |
41 |
Correct |
120 ms |
134212 KB |
Output is correct |
42 |
Correct |
119 ms |
134232 KB |
Output is correct |
43 |
Correct |
115 ms |
134220 KB |
Output is correct |
44 |
Correct |
120 ms |
134260 KB |
Output is correct |
45 |
Correct |
115 ms |
134252 KB |
Output is correct |
46 |
Correct |
117 ms |
134220 KB |
Output is correct |
47 |
Correct |
117 ms |
134228 KB |
Output is correct |
48 |
Correct |
116 ms |
134276 KB |
Output is correct |
49 |
Correct |
126 ms |
134416 KB |
Output is correct |
50 |
Correct |
119 ms |
134236 KB |
Output is correct |
51 |
Correct |
130 ms |
134288 KB |
Output is correct |
52 |
Correct |
116 ms |
134240 KB |
Output is correct |
53 |
Correct |
121 ms |
134348 KB |
Output is correct |
54 |
Correct |
116 ms |
134312 KB |
Output is correct |
55 |
Correct |
118 ms |
134220 KB |
Output is correct |