#include <bits/stdc++.h>
using namespace std;
#include <bits/extc++.h>
template <class T, class Comp = less<T>>
using ordered_set = __gnu_pbds::tree<
T, __gnu_pbds::null_type, Comp,
__gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update
>;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> perm(n), where(n);
for (int i = 0; i < n; ++i) {
cin >> perm[i];
where[--perm[i]] = i;
}
vector<int> ans(n + 1, n * n);
ans[0] = 0;
for (int i = 0; i < n; ++i) {
int segment = 0;
ordered_set<int> indices;
for (int j = 0; i + j < n; ++j) {
segment += where[i + j] - j + indices.order_of_key(where[i + j]);
ans[i + j + 1] = min(ans[i + j + 1], ans[i] + segment);
indices.insert(where[i + j]);
}
for (int j = i + 1; j < n; ++j) {
where[j] -= where[j] > where[i];
}
}
cout << ans[n] << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
2 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
2 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
2 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
4 ms |
364 KB |
Output is correct |
22 |
Correct |
4 ms |
364 KB |
Output is correct |
23 |
Correct |
4 ms |
492 KB |
Output is correct |
24 |
Correct |
4 ms |
364 KB |
Output is correct |
25 |
Correct |
4 ms |
364 KB |
Output is correct |
26 |
Correct |
4 ms |
364 KB |
Output is correct |
27 |
Correct |
4 ms |
364 KB |
Output is correct |
28 |
Correct |
5 ms |
364 KB |
Output is correct |
29 |
Correct |
4 ms |
364 KB |
Output is correct |
30 |
Correct |
4 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
2 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
4 ms |
364 KB |
Output is correct |
22 |
Correct |
4 ms |
364 KB |
Output is correct |
23 |
Correct |
4 ms |
492 KB |
Output is correct |
24 |
Correct |
4 ms |
364 KB |
Output is correct |
25 |
Correct |
4 ms |
364 KB |
Output is correct |
26 |
Correct |
4 ms |
364 KB |
Output is correct |
27 |
Correct |
4 ms |
364 KB |
Output is correct |
28 |
Correct |
5 ms |
364 KB |
Output is correct |
29 |
Correct |
4 ms |
364 KB |
Output is correct |
30 |
Correct |
4 ms |
364 KB |
Output is correct |
31 |
Correct |
60 ms |
364 KB |
Output is correct |
32 |
Correct |
60 ms |
364 KB |
Output is correct |
33 |
Correct |
62 ms |
364 KB |
Output is correct |
34 |
Correct |
60 ms |
364 KB |
Output is correct |
35 |
Correct |
60 ms |
364 KB |
Output is correct |
36 |
Correct |
59 ms |
492 KB |
Output is correct |
37 |
Correct |
60 ms |
364 KB |
Output is correct |
38 |
Correct |
60 ms |
364 KB |
Output is correct |
39 |
Correct |
59 ms |
364 KB |
Output is correct |
40 |
Correct |
60 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
2 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
4 ms |
364 KB |
Output is correct |
22 |
Correct |
4 ms |
364 KB |
Output is correct |
23 |
Correct |
4 ms |
492 KB |
Output is correct |
24 |
Correct |
4 ms |
364 KB |
Output is correct |
25 |
Correct |
4 ms |
364 KB |
Output is correct |
26 |
Correct |
4 ms |
364 KB |
Output is correct |
27 |
Correct |
4 ms |
364 KB |
Output is correct |
28 |
Correct |
5 ms |
364 KB |
Output is correct |
29 |
Correct |
4 ms |
364 KB |
Output is correct |
30 |
Correct |
4 ms |
364 KB |
Output is correct |
31 |
Correct |
60 ms |
364 KB |
Output is correct |
32 |
Correct |
60 ms |
364 KB |
Output is correct |
33 |
Correct |
62 ms |
364 KB |
Output is correct |
34 |
Correct |
60 ms |
364 KB |
Output is correct |
35 |
Correct |
60 ms |
364 KB |
Output is correct |
36 |
Correct |
59 ms |
492 KB |
Output is correct |
37 |
Correct |
60 ms |
364 KB |
Output is correct |
38 |
Correct |
60 ms |
364 KB |
Output is correct |
39 |
Correct |
59 ms |
364 KB |
Output is correct |
40 |
Correct |
60 ms |
364 KB |
Output is correct |
41 |
Correct |
3074 ms |
712 KB |
Output is correct |
42 |
Correct |
3067 ms |
712 KB |
Output is correct |
43 |
Correct |
3080 ms |
748 KB |
Output is correct |
44 |
Correct |
3071 ms |
748 KB |
Output is correct |
45 |
Correct |
3087 ms |
748 KB |
Output is correct |
46 |
Correct |
3084 ms |
712 KB |
Output is correct |
47 |
Correct |
3085 ms |
748 KB |
Output is correct |
48 |
Correct |
3076 ms |
748 KB |
Output is correct |
49 |
Correct |
3068 ms |
748 KB |
Output is correct |
50 |
Correct |
3097 ms |
712 KB |
Output is correct |
51 |
Correct |
3102 ms |
712 KB |
Output is correct |
52 |
Correct |
3092 ms |
748 KB |
Output is correct |
53 |
Correct |
3090 ms |
748 KB |
Output is correct |
54 |
Correct |
3085 ms |
712 KB |
Output is correct |
55 |
Correct |
3055 ms |
712 KB |
Output is correct |