#include <bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
const int N = 5e3 + 5;
const ll mod = 1e12 + 7;
ll n, m, a[N], b[N], x[N], dp[N], inv[N][N], tong;
void sol()
{
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
b[a[i]] = i;
}
for (int i = 1; i <= n; i++)
{
tong = 0;
for (int j = i - 1; j > 0; j--)
{
if (b[i] < b[j])
++tong;
inv[i][j] = inv[i - 1][j] + tong;
}
}
for (int i = 2; i <= n; i++)
{
dp[i] = mod;
for (int j = i - 1; j >= 0; j--)
{
dp[i] = min(dp[i], dp[j] + inv[i][1] - inv[j][1] + (i - j) * (i - j - 1) / 2 - inv[i][j + 1] * 2);
}
//cout << dp[i] << " ";
}
cout << dp[n];
}
int main()
{
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
int test = 1;
//cin >> test;
while (test-- > 0)
sol();
return 0;
}
/*
5
2 1 3 5 4
*/
// ctrl + F8 = compile
// ctrl + F9 = run
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
316 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
316 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
1216 KB |
Output is correct |
22 |
Correct |
1 ms |
1228 KB |
Output is correct |
23 |
Correct |
1 ms |
1228 KB |
Output is correct |
24 |
Correct |
1 ms |
1228 KB |
Output is correct |
25 |
Correct |
2 ms |
1300 KB |
Output is correct |
26 |
Correct |
1 ms |
1228 KB |
Output is correct |
27 |
Correct |
1 ms |
1228 KB |
Output is correct |
28 |
Correct |
1 ms |
1228 KB |
Output is correct |
29 |
Correct |
1 ms |
1228 KB |
Output is correct |
30 |
Correct |
1 ms |
1216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
316 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
1216 KB |
Output is correct |
22 |
Correct |
1 ms |
1228 KB |
Output is correct |
23 |
Correct |
1 ms |
1228 KB |
Output is correct |
24 |
Correct |
1 ms |
1228 KB |
Output is correct |
25 |
Correct |
2 ms |
1300 KB |
Output is correct |
26 |
Correct |
1 ms |
1228 KB |
Output is correct |
27 |
Correct |
1 ms |
1228 KB |
Output is correct |
28 |
Correct |
1 ms |
1228 KB |
Output is correct |
29 |
Correct |
1 ms |
1228 KB |
Output is correct |
30 |
Correct |
1 ms |
1216 KB |
Output is correct |
31 |
Correct |
6 ms |
5964 KB |
Output is correct |
32 |
Correct |
5 ms |
6092 KB |
Output is correct |
33 |
Correct |
5 ms |
6092 KB |
Output is correct |
34 |
Correct |
5 ms |
6092 KB |
Output is correct |
35 |
Correct |
5 ms |
6076 KB |
Output is correct |
36 |
Correct |
5 ms |
6092 KB |
Output is correct |
37 |
Correct |
5 ms |
6092 KB |
Output is correct |
38 |
Correct |
6 ms |
6092 KB |
Output is correct |
39 |
Correct |
5 ms |
6072 KB |
Output is correct |
40 |
Correct |
5 ms |
6108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
316 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
1216 KB |
Output is correct |
22 |
Correct |
1 ms |
1228 KB |
Output is correct |
23 |
Correct |
1 ms |
1228 KB |
Output is correct |
24 |
Correct |
1 ms |
1228 KB |
Output is correct |
25 |
Correct |
2 ms |
1300 KB |
Output is correct |
26 |
Correct |
1 ms |
1228 KB |
Output is correct |
27 |
Correct |
1 ms |
1228 KB |
Output is correct |
28 |
Correct |
1 ms |
1228 KB |
Output is correct |
29 |
Correct |
1 ms |
1228 KB |
Output is correct |
30 |
Correct |
1 ms |
1216 KB |
Output is correct |
31 |
Correct |
6 ms |
5964 KB |
Output is correct |
32 |
Correct |
5 ms |
6092 KB |
Output is correct |
33 |
Correct |
5 ms |
6092 KB |
Output is correct |
34 |
Correct |
5 ms |
6092 KB |
Output is correct |
35 |
Correct |
5 ms |
6076 KB |
Output is correct |
36 |
Correct |
5 ms |
6092 KB |
Output is correct |
37 |
Correct |
5 ms |
6092 KB |
Output is correct |
38 |
Correct |
6 ms |
6092 KB |
Output is correct |
39 |
Correct |
5 ms |
6072 KB |
Output is correct |
40 |
Correct |
5 ms |
6108 KB |
Output is correct |
41 |
Correct |
222 ms |
117432 KB |
Output is correct |
42 |
Correct |
217 ms |
117316 KB |
Output is correct |
43 |
Correct |
226 ms |
117372 KB |
Output is correct |
44 |
Correct |
221 ms |
117512 KB |
Output is correct |
45 |
Correct |
220 ms |
117280 KB |
Output is correct |
46 |
Correct |
216 ms |
117404 KB |
Output is correct |
47 |
Correct |
223 ms |
117624 KB |
Output is correct |
48 |
Correct |
221 ms |
117396 KB |
Output is correct |
49 |
Correct |
222 ms |
117476 KB |
Output is correct |
50 |
Correct |
222 ms |
117572 KB |
Output is correct |
51 |
Correct |
224 ms |
117300 KB |
Output is correct |
52 |
Correct |
219 ms |
117316 KB |
Output is correct |
53 |
Correct |
231 ms |
117520 KB |
Output is correct |
54 |
Correct |
219 ms |
117460 KB |
Output is correct |
55 |
Correct |
223 ms |
117380 KB |
Output is correct |