#include <iostream>
using namespace std;
int inv[5001][5001];
int main()
{
int N;
cin >> N;
int H[N+1], pos[N+1];
for(int i = 1; i <= N; i++) cin >> H[i];
for(int i = 1; i <= N; i++) pos[ H[i] ] = i;
int lowct[N+1][N+1]; //number of positions <= j with value greater than i
for(int i = 1; i <= N; i++)
{
lowct[i][0] = 0;
for(int j = 1; j <= N; j++)
{
lowct[i][j] = lowct[i][j-1] + (pos[j] > pos[i]);
}
}
for(int i = 1; i <= N; i++)
{
inv[i][i] = 0;
for(int j = i+1; j <= N; j++)
{
inv[i][j] = inv[i][j-1] + (lowct[j][j] - lowct[j][i-1]);
}
}
for(int j = 0; j <= N; j++) inv[0][j] = inv[1][j];
// for(int i = 1; i <= N; i++)
// {
// cout << pos[i] << ' ';
// }
// cout << '\n';
int dp[N+1];
dp[0] = 0;
for(int i = 1; i <= N; i++)
{
dp[i] = 1e9;
for(int l = 1; l <= i; l++)
{
dp[i] = min(dp[i], dp[i-l] + l*(l-1)/2 - inv[i-l+1][i] + inv[1][i] - inv[1][i-l] - inv[i-l+1][i]);
// cout << i-l+1 << ' ' << i << ' ' << l*(l-1)/2 - inv[i-l+1][i] << ' ' << inv[1][i] - inv[1][i-l] - inv[i-l+1][i] << ' ' << dp[i-l] << '\n';
}
// cout << dp[i] << ' ';
}
cout << dp[N] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
308 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
336 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
308 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
336 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
304 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 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 |
300 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 |
0 ms |
308 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
336 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
304 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 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 |
300 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 |
1232 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 |
1 ms |
1228 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 |
1356 KB |
Output is correct |
29 |
Correct |
1 ms |
1228 KB |
Output is correct |
30 |
Correct |
1 ms |
1228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
308 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
336 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
304 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 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 |
300 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 |
1232 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 |
1 ms |
1228 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 |
1356 KB |
Output is correct |
29 |
Correct |
1 ms |
1228 KB |
Output is correct |
30 |
Correct |
1 ms |
1228 KB |
Output is correct |
31 |
Correct |
6 ms |
7244 KB |
Output is correct |
32 |
Correct |
6 ms |
7212 KB |
Output is correct |
33 |
Correct |
8 ms |
7244 KB |
Output is correct |
34 |
Correct |
6 ms |
7244 KB |
Output is correct |
35 |
Correct |
6 ms |
7208 KB |
Output is correct |
36 |
Correct |
6 ms |
7216 KB |
Output is correct |
37 |
Correct |
6 ms |
7244 KB |
Output is correct |
38 |
Correct |
6 ms |
7244 KB |
Output is correct |
39 |
Correct |
6 ms |
7260 KB |
Output is correct |
40 |
Correct |
6 ms |
7244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
308 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
336 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
304 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 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 |
300 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 |
1232 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 |
1 ms |
1228 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 |
1356 KB |
Output is correct |
29 |
Correct |
1 ms |
1228 KB |
Output is correct |
30 |
Correct |
1 ms |
1228 KB |
Output is correct |
31 |
Correct |
6 ms |
7244 KB |
Output is correct |
32 |
Correct |
6 ms |
7212 KB |
Output is correct |
33 |
Correct |
8 ms |
7244 KB |
Output is correct |
34 |
Correct |
6 ms |
7244 KB |
Output is correct |
35 |
Correct |
6 ms |
7208 KB |
Output is correct |
36 |
Correct |
6 ms |
7216 KB |
Output is correct |
37 |
Correct |
6 ms |
7244 KB |
Output is correct |
38 |
Correct |
6 ms |
7244 KB |
Output is correct |
39 |
Correct |
6 ms |
7260 KB |
Output is correct |
40 |
Correct |
6 ms |
7244 KB |
Output is correct |
41 |
Correct |
392 ms |
165032 KB |
Output is correct |
42 |
Correct |
389 ms |
165084 KB |
Output is correct |
43 |
Correct |
392 ms |
165228 KB |
Output is correct |
44 |
Correct |
385 ms |
165056 KB |
Output is correct |
45 |
Correct |
341 ms |
165180 KB |
Output is correct |
46 |
Correct |
390 ms |
165136 KB |
Output is correct |
47 |
Correct |
392 ms |
165284 KB |
Output is correct |
48 |
Correct |
391 ms |
165196 KB |
Output is correct |
49 |
Correct |
399 ms |
165236 KB |
Output is correct |
50 |
Correct |
350 ms |
165192 KB |
Output is correct |
51 |
Correct |
394 ms |
165228 KB |
Output is correct |
52 |
Correct |
390 ms |
165192 KB |
Output is correct |
53 |
Correct |
396 ms |
165100 KB |
Output is correct |
54 |
Correct |
387 ms |
165140 KB |
Output is correct |
55 |
Correct |
350 ms |
165320 KB |
Output is correct |