This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
int dp[5010],a[5010][5010]={},ori[5010],pre[5010][5010]={};
int q(int li,int ri,int dj,int uj){
return pre[ri][uj]-pre[li-1][uj]-pre[ri][dj-1]+pre[dj-1][li-1];
}
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>ori[i];
dp[i]=1000000000;
}
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
if(ori[i]<ori[j]){
a[ori[i]][ori[j]]=1;
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
pre[i][j]=pre[i-1][j]+pre[i][j-1]-pre[i-1][j-1]+a[i][j];
}
}
for(int i=1;i<=n;i++){
dp[i]=dp[i-1]+i-1-q(1,i,i,i);
for(int j=0;j<i;j++){
dp[i]=min(dp[i],q(j+1,i,j+1,i)+(i-j)*j-q(1,j,j+1,i)+dp[j]);
}
}
cout<<dp[n]<<"\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |