#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
int n,a[5050],dp[5050],cnt[5050][5050],ind[5050];//cnt[i][j]=count of a[x] (x<i) s.t. a[x]>=j
ordered_set st;
int count(int l,int r){
return (cnt[r][n]-cnt[r][r])-(cnt[l-1][n]-cnt[l-1][r]);
}
int main(){
cin >> n;
for (int i=1;i<=n;i++) cin >> a[i],ind[a[i]]=i;
//for (int i=1;i<=n;i++) ind[i]+=ind[i-1];
for (int i=1;i<=n;i++){
for (int j=1;j<i;j++){
cnt[a[i]][a[j]]++,cnt[a[i]][n+1]--;
}
for (int j=1;j<=n+1;j++) cnt[a[i]][j]+=cnt[a[i]][j-1];
}
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++) cnt[i][j]+=cnt[i-1][j];
}
dp[0]=0;
ind[0]=INT_MAX;
for (int i=1;i<=n;i++){
dp[i]=INT_MAX;
int cnt=0;
st.clear();
st.insert(ind[i]);
for (int j=i-1;j>=0;j--){
dp[i]=min(dp[i],dp[j]+count(j+1,i)+cnt);
cnt+=st.size()-st.order_of_key(ind[j]);
st.insert(ind[j]);
}
}
cout << dp[n] << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
444 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
444 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
448 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
444 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
448 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
3 ms |
2904 KB |
Output is correct |
22 |
Correct |
3 ms |
2908 KB |
Output is correct |
23 |
Correct |
3 ms |
2908 KB |
Output is correct |
24 |
Correct |
3 ms |
2908 KB |
Output is correct |
25 |
Correct |
4 ms |
2908 KB |
Output is correct |
26 |
Correct |
3 ms |
2908 KB |
Output is correct |
27 |
Correct |
3 ms |
2908 KB |
Output is correct |
28 |
Correct |
3 ms |
2908 KB |
Output is correct |
29 |
Correct |
3 ms |
2908 KB |
Output is correct |
30 |
Correct |
3 ms |
2908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
444 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
448 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
3 ms |
2904 KB |
Output is correct |
22 |
Correct |
3 ms |
2908 KB |
Output is correct |
23 |
Correct |
3 ms |
2908 KB |
Output is correct |
24 |
Correct |
3 ms |
2908 KB |
Output is correct |
25 |
Correct |
4 ms |
2908 KB |
Output is correct |
26 |
Correct |
3 ms |
2908 KB |
Output is correct |
27 |
Correct |
3 ms |
2908 KB |
Output is correct |
28 |
Correct |
3 ms |
2908 KB |
Output is correct |
29 |
Correct |
3 ms |
2908 KB |
Output is correct |
30 |
Correct |
3 ms |
2908 KB |
Output is correct |
31 |
Correct |
57 ms |
15716 KB |
Output is correct |
32 |
Correct |
54 ms |
15196 KB |
Output is correct |
33 |
Correct |
53 ms |
15196 KB |
Output is correct |
34 |
Correct |
54 ms |
15452 KB |
Output is correct |
35 |
Correct |
54 ms |
15704 KB |
Output is correct |
36 |
Correct |
60 ms |
15696 KB |
Output is correct |
37 |
Correct |
53 ms |
15616 KB |
Output is correct |
38 |
Correct |
60 ms |
15872 KB |
Output is correct |
39 |
Correct |
53 ms |
15448 KB |
Output is correct |
40 |
Correct |
54 ms |
15604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
444 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
448 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
3 ms |
2904 KB |
Output is correct |
22 |
Correct |
3 ms |
2908 KB |
Output is correct |
23 |
Correct |
3 ms |
2908 KB |
Output is correct |
24 |
Correct |
3 ms |
2908 KB |
Output is correct |
25 |
Correct |
4 ms |
2908 KB |
Output is correct |
26 |
Correct |
3 ms |
2908 KB |
Output is correct |
27 |
Correct |
3 ms |
2908 KB |
Output is correct |
28 |
Correct |
3 ms |
2908 KB |
Output is correct |
29 |
Correct |
3 ms |
2908 KB |
Output is correct |
30 |
Correct |
3 ms |
2908 KB |
Output is correct |
31 |
Correct |
57 ms |
15716 KB |
Output is correct |
32 |
Correct |
54 ms |
15196 KB |
Output is correct |
33 |
Correct |
53 ms |
15196 KB |
Output is correct |
34 |
Correct |
54 ms |
15452 KB |
Output is correct |
35 |
Correct |
54 ms |
15704 KB |
Output is correct |
36 |
Correct |
60 ms |
15696 KB |
Output is correct |
37 |
Correct |
53 ms |
15616 KB |
Output is correct |
38 |
Correct |
60 ms |
15872 KB |
Output is correct |
39 |
Correct |
53 ms |
15448 KB |
Output is correct |
40 |
Correct |
54 ms |
15604 KB |
Output is correct |
41 |
Correct |
2712 ms |
99776 KB |
Output is correct |
42 |
Correct |
2697 ms |
99856 KB |
Output is correct |
43 |
Correct |
2759 ms |
99620 KB |
Output is correct |
44 |
Correct |
2797 ms |
99792 KB |
Output is correct |
45 |
Correct |
2781 ms |
99504 KB |
Output is correct |
46 |
Correct |
2886 ms |
99480 KB |
Output is correct |
47 |
Correct |
2692 ms |
99796 KB |
Output is correct |
48 |
Correct |
2683 ms |
99724 KB |
Output is correct |
49 |
Correct |
2721 ms |
99528 KB |
Output is correct |
50 |
Correct |
2704 ms |
99412 KB |
Output is correct |
51 |
Correct |
2685 ms |
99416 KB |
Output is correct |
52 |
Correct |
2727 ms |
99924 KB |
Output is correct |
53 |
Correct |
2707 ms |
99796 KB |
Output is correct |
54 |
Correct |
2702 ms |
99684 KB |
Output is correct |
55 |
Correct |
2695 ms |
99728 KB |
Output is correct |