#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=5050;
const int inf=1e17;
int n,T[N];
int a[N],ind[N],dp[N];
void Update(int i,int qval){
while(i<=n){
T[i]+=qval;
i+=i&(-i);
}
}
int Get(int i){
int sum=0;
while(i>=1){
sum+=T[i];
i-=i&(-i);
}
return sum;
}
/*int lc[2*N],rc[2*N],sum[2*N],nc,root;
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()
{
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(ind[j],1);
for(int j=i;j<=n;j++) b[j]=Get(ind[j])-1;
for(int j=i;j<=n;j++) Update(ind[j],-1);
for(int j=i;j<=n;j++){
int k=ind[j],x=Get(n)-Get(k-1);
s-=x;
s+=b[j];
dp[i]=min(dp[i],dp[j+1]+s);
Update(k,1);
}
for(int j=i;j<=n;j++) Update(ind[j],-1);
/*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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
600 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 |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
600 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 |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
600 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 |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
600 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 |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
13 ms |
344 KB |
Output is correct |
32 |
Correct |
12 ms |
476 KB |
Output is correct |
33 |
Correct |
11 ms |
344 KB |
Output is correct |
34 |
Correct |
11 ms |
348 KB |
Output is correct |
35 |
Correct |
11 ms |
472 KB |
Output is correct |
36 |
Correct |
11 ms |
348 KB |
Output is correct |
37 |
Correct |
13 ms |
348 KB |
Output is correct |
38 |
Correct |
11 ms |
348 KB |
Output is correct |
39 |
Correct |
11 ms |
724 KB |
Output is correct |
40 |
Correct |
11 ms |
472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
600 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 |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
13 ms |
344 KB |
Output is correct |
32 |
Correct |
12 ms |
476 KB |
Output is correct |
33 |
Correct |
11 ms |
344 KB |
Output is correct |
34 |
Correct |
11 ms |
348 KB |
Output is correct |
35 |
Correct |
11 ms |
472 KB |
Output is correct |
36 |
Correct |
11 ms |
348 KB |
Output is correct |
37 |
Correct |
13 ms |
348 KB |
Output is correct |
38 |
Correct |
11 ms |
348 KB |
Output is correct |
39 |
Correct |
11 ms |
724 KB |
Output is correct |
40 |
Correct |
11 ms |
472 KB |
Output is correct |
41 |
Correct |
871 ms |
856 KB |
Output is correct |
42 |
Correct |
874 ms |
628 KB |
Output is correct |
43 |
Correct |
870 ms |
624 KB |
Output is correct |
44 |
Correct |
872 ms |
628 KB |
Output is correct |
45 |
Correct |
870 ms |
604 KB |
Output is correct |
46 |
Correct |
863 ms |
628 KB |
Output is correct |
47 |
Correct |
862 ms |
640 KB |
Output is correct |
48 |
Correct |
876 ms |
624 KB |
Output is correct |
49 |
Correct |
868 ms |
852 KB |
Output is correct |
50 |
Correct |
864 ms |
628 KB |
Output is correct |
51 |
Correct |
862 ms |
848 KB |
Output is correct |
52 |
Correct |
859 ms |
852 KB |
Output is correct |
53 |
Correct |
860 ms |
644 KB |
Output is correct |
54 |
Correct |
876 ms |
600 KB |
Output is correct |
55 |
Correct |
865 ms |
624 KB |
Output is correct |