#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=5050;
const int inf=1e17;
int lc[2*N],rc[2*N],sum[2*N],nc,root;
int a[N],ind[N],dp[N];
void Update(int &c,int ss,int se,int qind,int qval){
if(!c) c=++nc;
if(ss==se) {sum[c]=qval;return;}
int mid=(ss+se)/2;
if(qind<=mid) Update(lc[c],ss,mid,qind,qval);
else Update(rc[c],mid+1,se,qind,qval);
sum[c]=sum[lc[c]]+sum[rc[c]];
}
int Get(int c,int ss,int se,int qs,int qe){
if(se<ss) return 0;
if(qs<=ss && se<=qe) return sum[c];
else if(qe<ss || se<qs) return 0;
int mid=(ss+se)/2;
return Get(lc[c],ss,mid,qs,qe)+Get(rc[c],mid+1,se,qs,qe);
}
signed main()
{
int n;cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
ind[a[i]]=i;
}
for(int i=1;i<=n;i++) dp[i]=inf;
dp[n+1]=0;
for(int i=n;i>=1;i--){
int s=0,b[n+1]={0};
for(int j=i;j<=n;j++) Update(root,1,n,ind[j],1);
for(int j=i;j<=n;j++) b[j]=Get(root,1,n,1,ind[j])-1;
for(int j=i;j<=n;j++) Update(root,1,n,ind[j],0);
for(int j=i;j<=n;j++){
int k=ind[j],x=Get(root,1,n,k,n);
s-=x;
s+=b[j];
dp[i]=min(dp[i],dp[j+1]+s);
Update(root,1,n,k,1);
}
for(int j=i;j<=n;j++) Update(root,1,n,ind[j],0);
}
cout<<dp[1]<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
444 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
444 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
444 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
5 ms |
348 KB |
Output is correct |
22 |
Correct |
5 ms |
472 KB |
Output is correct |
23 |
Correct |
5 ms |
348 KB |
Output is correct |
24 |
Correct |
5 ms |
348 KB |
Output is correct |
25 |
Correct |
5 ms |
348 KB |
Output is correct |
26 |
Correct |
5 ms |
344 KB |
Output is correct |
27 |
Correct |
5 ms |
348 KB |
Output is correct |
28 |
Correct |
5 ms |
348 KB |
Output is correct |
29 |
Correct |
5 ms |
348 KB |
Output is correct |
30 |
Correct |
5 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
444 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
5 ms |
348 KB |
Output is correct |
22 |
Correct |
5 ms |
472 KB |
Output is correct |
23 |
Correct |
5 ms |
348 KB |
Output is correct |
24 |
Correct |
5 ms |
348 KB |
Output is correct |
25 |
Correct |
5 ms |
348 KB |
Output is correct |
26 |
Correct |
5 ms |
344 KB |
Output is correct |
27 |
Correct |
5 ms |
348 KB |
Output is correct |
28 |
Correct |
5 ms |
348 KB |
Output is correct |
29 |
Correct |
5 ms |
348 KB |
Output is correct |
30 |
Correct |
5 ms |
348 KB |
Output is correct |
31 |
Correct |
149 ms |
752 KB |
Output is correct |
32 |
Correct |
152 ms |
492 KB |
Output is correct |
33 |
Correct |
158 ms |
492 KB |
Output is correct |
34 |
Correct |
146 ms |
492 KB |
Output is correct |
35 |
Correct |
146 ms |
344 KB |
Output is correct |
36 |
Correct |
146 ms |
492 KB |
Output is correct |
37 |
Correct |
145 ms |
480 KB |
Output is correct |
38 |
Correct |
150 ms |
496 KB |
Output is correct |
39 |
Correct |
148 ms |
752 KB |
Output is correct |
40 |
Correct |
146 ms |
488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
444 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
5 ms |
348 KB |
Output is correct |
22 |
Correct |
5 ms |
472 KB |
Output is correct |
23 |
Correct |
5 ms |
348 KB |
Output is correct |
24 |
Correct |
5 ms |
348 KB |
Output is correct |
25 |
Correct |
5 ms |
348 KB |
Output is correct |
26 |
Correct |
5 ms |
344 KB |
Output is correct |
27 |
Correct |
5 ms |
348 KB |
Output is correct |
28 |
Correct |
5 ms |
348 KB |
Output is correct |
29 |
Correct |
5 ms |
348 KB |
Output is correct |
30 |
Correct |
5 ms |
348 KB |
Output is correct |
31 |
Correct |
149 ms |
752 KB |
Output is correct |
32 |
Correct |
152 ms |
492 KB |
Output is correct |
33 |
Correct |
158 ms |
492 KB |
Output is correct |
34 |
Correct |
146 ms |
492 KB |
Output is correct |
35 |
Correct |
146 ms |
344 KB |
Output is correct |
36 |
Correct |
146 ms |
492 KB |
Output is correct |
37 |
Correct |
145 ms |
480 KB |
Output is correct |
38 |
Correct |
150 ms |
496 KB |
Output is correct |
39 |
Correct |
148 ms |
752 KB |
Output is correct |
40 |
Correct |
146 ms |
488 KB |
Output is correct |
41 |
Execution timed out |
5026 ms |
1204 KB |
Time limit exceeded |
42 |
Halted |
0 ms |
0 KB |
- |