#include <bits/stdc++.h>
using namespace std;
struct bit{
int b[5010];
void cl(){
for (int i=0; i<5010; i++) b[i]=0;
}
void update(int pos,int val){
for (int i=pos; i<5010; i+=i&(-i)) b[i]+=val;
}
int query(int pos){
int ans=0;
for (int i=pos; i; i-=i&(-i)) ans+=b[i];
return ans;
}
};
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int n; cin>>n;
int a[n+1];
for (int i=1; i<=n; i++) cin>>a[i];
int pos[n+1];
for (int i=1; i<=n; i++) pos[a[i]]=i;
int cost[n+1][n+1];
bit out,in;
for (int i=1; i<=n; i++){
out.cl(); in.cl();
for (int j=i+1; j<=n; j++) out.update(pos[j],1);
int cnt=0;
for (int j=i; j>0; j--){
cnt+=out.query(pos[j]);
cnt+=in.query(n+1-pos[j]);
in.update(n+1-pos[j],1);
cost[j][i]=cnt;
}
}
int dp[n+1];
dp[0]=0;
for (int i=1; i<=n; i++) dp[i]=1e9;
for (int i=1; i<=n; i++){
for (int j=0; j<i; j++) dp[i]=min(dp[i],dp[j]+cost[j+1][i]);
}
cout<<dp[n]<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
356 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
356 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 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 |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
352 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
356 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 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 |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
352 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
2 ms |
480 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
480 KB |
Output is correct |
25 |
Correct |
1 ms |
480 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
27 |
Correct |
2 ms |
488 KB |
Output is correct |
28 |
Correct |
2 ms |
480 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
356 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 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 |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
352 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
2 ms |
480 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
480 KB |
Output is correct |
25 |
Correct |
1 ms |
480 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
27 |
Correct |
2 ms |
488 KB |
Output is correct |
28 |
Correct |
2 ms |
480 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
31 |
Correct |
16 ms |
2864 KB |
Output is correct |
32 |
Correct |
18 ms |
2872 KB |
Output is correct |
33 |
Correct |
19 ms |
2772 KB |
Output is correct |
34 |
Correct |
17 ms |
2880 KB |
Output is correct |
35 |
Correct |
18 ms |
2864 KB |
Output is correct |
36 |
Correct |
16 ms |
2880 KB |
Output is correct |
37 |
Correct |
17 ms |
2772 KB |
Output is correct |
38 |
Correct |
17 ms |
2880 KB |
Output is correct |
39 |
Correct |
17 ms |
2772 KB |
Output is correct |
40 |
Correct |
17 ms |
2772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
356 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 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 |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
352 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
2 ms |
480 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
480 KB |
Output is correct |
25 |
Correct |
1 ms |
480 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
27 |
Correct |
2 ms |
488 KB |
Output is correct |
28 |
Correct |
2 ms |
480 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
31 |
Correct |
16 ms |
2864 KB |
Output is correct |
32 |
Correct |
18 ms |
2872 KB |
Output is correct |
33 |
Correct |
19 ms |
2772 KB |
Output is correct |
34 |
Correct |
17 ms |
2880 KB |
Output is correct |
35 |
Correct |
18 ms |
2864 KB |
Output is correct |
36 |
Correct |
16 ms |
2880 KB |
Output is correct |
37 |
Correct |
17 ms |
2772 KB |
Output is correct |
38 |
Correct |
17 ms |
2880 KB |
Output is correct |
39 |
Correct |
17 ms |
2772 KB |
Output is correct |
40 |
Correct |
17 ms |
2772 KB |
Output is correct |
41 |
Correct |
737 ms |
98296 KB |
Output is correct |
42 |
Correct |
739 ms |
98204 KB |
Output is correct |
43 |
Correct |
729 ms |
98272 KB |
Output is correct |
44 |
Correct |
732 ms |
98244 KB |
Output is correct |
45 |
Correct |
735 ms |
98244 KB |
Output is correct |
46 |
Correct |
725 ms |
98380 KB |
Output is correct |
47 |
Correct |
764 ms |
98300 KB |
Output is correct |
48 |
Correct |
747 ms |
98304 KB |
Output is correct |
49 |
Correct |
739 ms |
98380 KB |
Output is correct |
50 |
Correct |
741 ms |
98304 KB |
Output is correct |
51 |
Correct |
751 ms |
98364 KB |
Output is correct |
52 |
Correct |
751 ms |
98412 KB |
Output is correct |
53 |
Correct |
734 ms |
98292 KB |
Output is correct |
54 |
Correct |
743 ms |
98284 KB |
Output is correct |
55 |
Correct |
736 ms |
98396 KB |
Output is correct |