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;
const int N=505;
int n,h[N];
int mn(int i, int j){
if(i!=-1&&j!=-1) return min(i,j);
else return max(i,j);
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin>>n;
for(int i=1; i<=n; i++) cin>>h[i];
h[0]=0;
int ord[n+5];
for(int i=1; i<=n; i++) ord[h[i]]=i;
int supp[N][N];
memset(supp,0,sizeof(supp));
for(int i=n-1; i>=1; i--){
for(int j=0; j<=h[i]; j++) supp[i][j]=supp[i][j+1]+1;
for(int j=h[i]+1; j<N; j++) supp[i][j]=supp[i][j+1];
}
int dp[N];
memset(dp,-1,sizeof(dp));
dp[0]=0;
for(int i=1; i<=n; i++){
int trans=0;
for(int j=i; j>0; j--){
trans += supp[ord[j]][j];
trans -= supp[ord[j]][i];
trans -= supp[ord[j]][i];
trans += N-i;
dp[i]=mn(dp[i],dp[j]+trans);
}
}
cout<<dp[n];
}
# | 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... |