#include <bits/stdc++.h>
using namespace std;
const int maxn = 5005;
const int inf = 1e9;
int n;
int p[maxn];
int pos[maxn];
int dp[maxn];
int tree_original[maxn];
void update_original(int idx, int val)
{
for (int i = idx; i <= n; i += (i & (-i)))
{
tree_original[i] += val;
}
}
int query_original(int idx)
{
int result = 0;
for (int i = idx; i >= 1; i -= (i & (-i)))
{
result += tree_original[i];
}
return result;
}
void fastIO()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main()
{
fastIO();
cin >> n;
for (int i = 1; i <= n; ++i)
{
cin >> p[i];
pos[p[i]] = i;
}
dp[n + 1] = 0;
for (int i = n; i >= 1; --i)
{
update_original(pos[i], +1);
dp[i] = 1e9;
int cost = 0;
for (int j = i; j <= n; ++j)
{
cost += query_original(pos[j]) - 1;
for (int k = 1; k <= n; ++k)
{
if (pos[k] > pos[j] && k >= i && k <= j)
{
--cost;
}
}
dp[i] = min(dp[i], dp[j + 1] + cost);
}
}
cout << dp[1] << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 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 |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 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 |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 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 |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
7 ms |
340 KB |
Output is correct |
22 |
Correct |
7 ms |
336 KB |
Output is correct |
23 |
Correct |
7 ms |
340 KB |
Output is correct |
24 |
Correct |
8 ms |
340 KB |
Output is correct |
25 |
Correct |
8 ms |
340 KB |
Output is correct |
26 |
Correct |
8 ms |
340 KB |
Output is correct |
27 |
Correct |
8 ms |
344 KB |
Output is correct |
28 |
Correct |
9 ms |
336 KB |
Output is correct |
29 |
Correct |
7 ms |
324 KB |
Output is correct |
30 |
Correct |
8 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 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 |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
7 ms |
340 KB |
Output is correct |
22 |
Correct |
7 ms |
336 KB |
Output is correct |
23 |
Correct |
7 ms |
340 KB |
Output is correct |
24 |
Correct |
8 ms |
340 KB |
Output is correct |
25 |
Correct |
8 ms |
340 KB |
Output is correct |
26 |
Correct |
8 ms |
340 KB |
Output is correct |
27 |
Correct |
8 ms |
344 KB |
Output is correct |
28 |
Correct |
9 ms |
336 KB |
Output is correct |
29 |
Correct |
7 ms |
324 KB |
Output is correct |
30 |
Correct |
8 ms |
340 KB |
Output is correct |
31 |
Correct |
580 ms |
340 KB |
Output is correct |
32 |
Correct |
579 ms |
340 KB |
Output is correct |
33 |
Correct |
579 ms |
340 KB |
Output is correct |
34 |
Correct |
588 ms |
340 KB |
Output is correct |
35 |
Correct |
580 ms |
340 KB |
Output is correct |
36 |
Correct |
595 ms |
340 KB |
Output is correct |
37 |
Correct |
570 ms |
340 KB |
Output is correct |
38 |
Correct |
596 ms |
452 KB |
Output is correct |
39 |
Correct |
609 ms |
340 KB |
Output is correct |
40 |
Correct |
594 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 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 |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
7 ms |
340 KB |
Output is correct |
22 |
Correct |
7 ms |
336 KB |
Output is correct |
23 |
Correct |
7 ms |
340 KB |
Output is correct |
24 |
Correct |
8 ms |
340 KB |
Output is correct |
25 |
Correct |
8 ms |
340 KB |
Output is correct |
26 |
Correct |
8 ms |
340 KB |
Output is correct |
27 |
Correct |
8 ms |
344 KB |
Output is correct |
28 |
Correct |
9 ms |
336 KB |
Output is correct |
29 |
Correct |
7 ms |
324 KB |
Output is correct |
30 |
Correct |
8 ms |
340 KB |
Output is correct |
31 |
Correct |
580 ms |
340 KB |
Output is correct |
32 |
Correct |
579 ms |
340 KB |
Output is correct |
33 |
Correct |
579 ms |
340 KB |
Output is correct |
34 |
Correct |
588 ms |
340 KB |
Output is correct |
35 |
Correct |
580 ms |
340 KB |
Output is correct |
36 |
Correct |
595 ms |
340 KB |
Output is correct |
37 |
Correct |
570 ms |
340 KB |
Output is correct |
38 |
Correct |
596 ms |
452 KB |
Output is correct |
39 |
Correct |
609 ms |
340 KB |
Output is correct |
40 |
Correct |
594 ms |
340 KB |
Output is correct |
41 |
Execution timed out |
5063 ms |
388 KB |
Time limit exceeded |
42 |
Halted |
0 ms |
0 KB |
- |