//#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
#define endl '\n'
llo n;
llo it[5001];
llo dp[5001];
llo ind[5001];
llo cost[5001][5001];
llo pre[5001][5001];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
for(llo i=0;i<n;i++){
cin>>it[i];
ind[it[i]]=i;
}
for(llo i=1;i<=n;i++){
for(llo j=1;j<=n;j++){
pre[i][j]=pre[i][j-1];
if(ind[j]>ind[i]){
pre[i][j]++;
}
}
}
/*for(int i=1;i<=3;i++){
cout<<pre[3][i]<<",,";
}
cout<<endl;*/
for(llo i=1;i<=n;i++){
for(llo j=i;j<=n;j++){
cost[i][j]=0;
if(i<j){
cost[i][j]+=cost[i][j-1];
}
cost[i][j]+=pre[j][i-1];
if(i<j){
cost[i][j]+=(j-i)-(pre[j][j-1]-pre[j][i-1]);
}
}
}
/*cout<<cost[1][1]<<":"<<cost[1][2]<<":"<<cost[2][3]<<endl;
cout<<cost[1][3]<<":"<<cost[4][5]<<endl;*/
for(llo i=0;i<=n;i++){
dp[i]=1e9;
}
dp[0]=0;
for(llo i=1;i<=n;i++){
for(llo j=1;j<=i;j++){
dp[i]=min(dp[i],dp[j-1]+cost[j][i]);
}
/*for(llo j=0;j<n;j++){
for(llo k=0;k<n;k++){
}
}*/
}
cout<<dp[n]<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
1 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 |
1 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 |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
620 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 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 |
1 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 |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
620 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
2 ms |
2412 KB |
Output is correct |
22 |
Correct |
2 ms |
2412 KB |
Output is correct |
23 |
Correct |
2 ms |
2412 KB |
Output is correct |
24 |
Correct |
3 ms |
2412 KB |
Output is correct |
25 |
Correct |
83 ms |
2412 KB |
Output is correct |
26 |
Correct |
2 ms |
2412 KB |
Output is correct |
27 |
Correct |
2 ms |
2412 KB |
Output is correct |
28 |
Correct |
5 ms |
2412 KB |
Output is correct |
29 |
Correct |
126 ms |
2412 KB |
Output is correct |
30 |
Correct |
4 ms |
2412 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 |
1 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 |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
620 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
2 ms |
2412 KB |
Output is correct |
22 |
Correct |
2 ms |
2412 KB |
Output is correct |
23 |
Correct |
2 ms |
2412 KB |
Output is correct |
24 |
Correct |
3 ms |
2412 KB |
Output is correct |
25 |
Correct |
83 ms |
2412 KB |
Output is correct |
26 |
Correct |
2 ms |
2412 KB |
Output is correct |
27 |
Correct |
2 ms |
2412 KB |
Output is correct |
28 |
Correct |
5 ms |
2412 KB |
Output is correct |
29 |
Correct |
126 ms |
2412 KB |
Output is correct |
30 |
Correct |
4 ms |
2412 KB |
Output is correct |
31 |
Correct |
17 ms |
14444 KB |
Output is correct |
32 |
Correct |
27 ms |
14316 KB |
Output is correct |
33 |
Correct |
27 ms |
14316 KB |
Output is correct |
34 |
Correct |
26 ms |
14444 KB |
Output is correct |
35 |
Correct |
17 ms |
14444 KB |
Output is correct |
36 |
Correct |
14 ms |
14444 KB |
Output is correct |
37 |
Correct |
14 ms |
14444 KB |
Output is correct |
38 |
Correct |
14 ms |
14444 KB |
Output is correct |
39 |
Correct |
14 ms |
14444 KB |
Output is correct |
40 |
Correct |
14 ms |
14348 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 |
1 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 |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
620 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
2 ms |
2412 KB |
Output is correct |
22 |
Correct |
2 ms |
2412 KB |
Output is correct |
23 |
Correct |
2 ms |
2412 KB |
Output is correct |
24 |
Correct |
3 ms |
2412 KB |
Output is correct |
25 |
Correct |
83 ms |
2412 KB |
Output is correct |
26 |
Correct |
2 ms |
2412 KB |
Output is correct |
27 |
Correct |
2 ms |
2412 KB |
Output is correct |
28 |
Correct |
5 ms |
2412 KB |
Output is correct |
29 |
Correct |
126 ms |
2412 KB |
Output is correct |
30 |
Correct |
4 ms |
2412 KB |
Output is correct |
31 |
Correct |
17 ms |
14444 KB |
Output is correct |
32 |
Correct |
27 ms |
14316 KB |
Output is correct |
33 |
Correct |
27 ms |
14316 KB |
Output is correct |
34 |
Correct |
26 ms |
14444 KB |
Output is correct |
35 |
Correct |
17 ms |
14444 KB |
Output is correct |
36 |
Correct |
14 ms |
14444 KB |
Output is correct |
37 |
Correct |
14 ms |
14444 KB |
Output is correct |
38 |
Correct |
14 ms |
14444 KB |
Output is correct |
39 |
Correct |
14 ms |
14444 KB |
Output is correct |
40 |
Correct |
14 ms |
14348 KB |
Output is correct |
41 |
Correct |
717 ms |
313068 KB |
Output is correct |
42 |
Correct |
715 ms |
312976 KB |
Output is correct |
43 |
Correct |
781 ms |
313316 KB |
Output is correct |
44 |
Correct |
713 ms |
313152 KB |
Output is correct |
45 |
Correct |
736 ms |
313256 KB |
Output is correct |
46 |
Correct |
727 ms |
313228 KB |
Output is correct |
47 |
Correct |
729 ms |
313196 KB |
Output is correct |
48 |
Correct |
730 ms |
313324 KB |
Output is correct |
49 |
Correct |
718 ms |
313324 KB |
Output is correct |
50 |
Correct |
736 ms |
313224 KB |
Output is correct |
51 |
Correct |
715 ms |
313140 KB |
Output is correct |
52 |
Correct |
748 ms |
313324 KB |
Output is correct |
53 |
Correct |
726 ms |
313252 KB |
Output is correct |
54 |
Correct |
773 ms |
313280 KB |
Output is correct |
55 |
Correct |
726 ms |
313196 KB |
Output is correct |