#include<bits/stdc++.h>
#define MAXN 307
using namespace std;
int n,a[MAXN];
int dp[MAXN][MAXN][MAXN];
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int len=1;len<=n;len++){
for(int l=1;l+len-1<=n;l++){
int r=l+len-1;
int used=n-len;
for(int pref=0;pref<=used;pref++){
if(len==1){
dp[l][r][pref]=(a[l]==pref+1);
}else{
dp[l][r][pref]=max( max(dp[l+1][r][pref] , dp[l][r-1][pref+1]) , max(dp[l+1][r][pref] , dp[l][r-1][pref]) );
if(pref+1==a[l])dp[l][r][pref]=max(dp[l][r][pref],dp[l+1][r][pref+1]+1);
if(pref+1==a[r])dp[l][r][pref]=max(dp[l][r][pref],dp[l][r-1][pref+1]+1);
if(n-(used-pref)==a[l])dp[l][r][pref]=max(dp[l][r][pref],dp[l+1][r][pref]+1);
if(n-(used-pref)==a[r])dp[l][r][pref]=max(dp[l][r][pref],dp[l][r-1][pref]+1);
}
}
}
}
cout<<n-dp[1][n][0]<<"\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... |