#include <bits/stdc++.h>
using namespace std;
void calculate_prefix();
constexpr static int INF = 1e8;
constexpr static int MXSIZE = 5000;
int prefix[MXSIZE+1][MXSIZE+1], v[MXSIZE], dp[MXSIZE+1][MXSIZE+1], n, p[MXSIZE];
int get(int x1, int y1, int x2, int y2);
int main()
{
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> v[i];
v[i]--;
p[v[i]] = i;
}
for (int i = 0; i < n; i++)
prefix[i+1][v[i]+1] = 1;
calculate_prefix();
dp[0][0] = p[0];
for (int i = 1; i < n; i++)
{
dp[i][i] = INF;
for (int j = 0; j < i; j++)
dp[i][i] = min(dp[i][i], dp[i-1][j]);
dp[i][i] += get(0, i, p[i], n);
for (int j = 0; j < i; j++)
dp[i][j] = dp[i-1][j] - get(p[i], j, n, i) + get(0, j, p[i], n);
}
int best = INF;
for (int i = 0; i < n; i++)
best = min(best, dp[n-1][i]);
#ifdef DEBUGGING
for (int i = 0; i < n; i++)
for (int j = 0; j <= i; j++)
cout << (i+1) << " " << (j+1) << ": " << dp[i][j] << "\n";
#endif
cout << best <<"\n";
}
void calculate_prefix() // O(n^2)
{
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
prefix[i][j] += prefix[i][j-1] + prefix[i-1][j] - prefix[i-1][j-1];
}
int get(int x1, int y1, int x2, int y2) // O(1)
{
return prefix[x2][y2] - prefix[x2][y1] - prefix[x1][y2] + prefix[x1][y1];
}
# |
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 |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 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 |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
440 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
444 KB |
Output is correct |
18 |
Correct |
0 ms |
440 KB |
Output is correct |
19 |
Correct |
1 ms |
468 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 |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
440 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
444 KB |
Output is correct |
18 |
Correct |
0 ms |
440 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
2132 KB |
Output is correct |
22 |
Correct |
1 ms |
2104 KB |
Output is correct |
23 |
Correct |
2 ms |
2132 KB |
Output is correct |
24 |
Correct |
1 ms |
2132 KB |
Output is correct |
25 |
Correct |
2 ms |
2132 KB |
Output is correct |
26 |
Correct |
2 ms |
2132 KB |
Output is correct |
27 |
Correct |
2 ms |
2132 KB |
Output is correct |
28 |
Correct |
1 ms |
2132 KB |
Output is correct |
29 |
Correct |
1 ms |
2132 KB |
Output is correct |
30 |
Correct |
2 ms |
2132 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 |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
440 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
444 KB |
Output is correct |
18 |
Correct |
0 ms |
440 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
2132 KB |
Output is correct |
22 |
Correct |
1 ms |
2104 KB |
Output is correct |
23 |
Correct |
2 ms |
2132 KB |
Output is correct |
24 |
Correct |
1 ms |
2132 KB |
Output is correct |
25 |
Correct |
2 ms |
2132 KB |
Output is correct |
26 |
Correct |
2 ms |
2132 KB |
Output is correct |
27 |
Correct |
2 ms |
2132 KB |
Output is correct |
28 |
Correct |
1 ms |
2132 KB |
Output is correct |
29 |
Correct |
1 ms |
2132 KB |
Output is correct |
30 |
Correct |
2 ms |
2132 KB |
Output is correct |
31 |
Correct |
7 ms |
10452 KB |
Output is correct |
32 |
Correct |
7 ms |
10452 KB |
Output is correct |
33 |
Correct |
7 ms |
10452 KB |
Output is correct |
34 |
Correct |
6 ms |
10452 KB |
Output is correct |
35 |
Correct |
7 ms |
10424 KB |
Output is correct |
36 |
Correct |
7 ms |
10452 KB |
Output is correct |
37 |
Correct |
7 ms |
10452 KB |
Output is correct |
38 |
Correct |
7 ms |
10452 KB |
Output is correct |
39 |
Correct |
7 ms |
10452 KB |
Output is correct |
40 |
Correct |
7 ms |
10452 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 |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
440 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
444 KB |
Output is correct |
18 |
Correct |
0 ms |
440 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
2132 KB |
Output is correct |
22 |
Correct |
1 ms |
2104 KB |
Output is correct |
23 |
Correct |
2 ms |
2132 KB |
Output is correct |
24 |
Correct |
1 ms |
2132 KB |
Output is correct |
25 |
Correct |
2 ms |
2132 KB |
Output is correct |
26 |
Correct |
2 ms |
2132 KB |
Output is correct |
27 |
Correct |
2 ms |
2132 KB |
Output is correct |
28 |
Correct |
1 ms |
2132 KB |
Output is correct |
29 |
Correct |
1 ms |
2132 KB |
Output is correct |
30 |
Correct |
2 ms |
2132 KB |
Output is correct |
31 |
Correct |
7 ms |
10452 KB |
Output is correct |
32 |
Correct |
7 ms |
10452 KB |
Output is correct |
33 |
Correct |
7 ms |
10452 KB |
Output is correct |
34 |
Correct |
6 ms |
10452 KB |
Output is correct |
35 |
Correct |
7 ms |
10424 KB |
Output is correct |
36 |
Correct |
7 ms |
10452 KB |
Output is correct |
37 |
Correct |
7 ms |
10452 KB |
Output is correct |
38 |
Correct |
7 ms |
10452 KB |
Output is correct |
39 |
Correct |
7 ms |
10452 KB |
Output is correct |
40 |
Correct |
7 ms |
10452 KB |
Output is correct |
41 |
Correct |
160 ms |
165028 KB |
Output is correct |
42 |
Correct |
151 ms |
165028 KB |
Output is correct |
43 |
Correct |
151 ms |
165036 KB |
Output is correct |
44 |
Correct |
151 ms |
165144 KB |
Output is correct |
45 |
Correct |
154 ms |
165100 KB |
Output is correct |
46 |
Correct |
164 ms |
165092 KB |
Output is correct |
47 |
Correct |
151 ms |
165156 KB |
Output is correct |
48 |
Correct |
152 ms |
165152 KB |
Output is correct |
49 |
Correct |
154 ms |
165244 KB |
Output is correct |
50 |
Correct |
152 ms |
165180 KB |
Output is correct |
51 |
Correct |
164 ms |
165072 KB |
Output is correct |
52 |
Correct |
152 ms |
165116 KB |
Output is correct |
53 |
Correct |
157 ms |
165096 KB |
Output is correct |
54 |
Correct |
150 ms |
165116 KB |
Output is correct |
55 |
Correct |
152 ms |
165168 KB |
Output is correct |