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;
#define ll long long
#define pii pair<int, int>
#define pb push_back
#define ff first
#define ss second
int a[5001];
int cnt[5001][5001];
int dp[5001];
int ddp[5001];
int pos[200100];
void solve(){
int n;
cin>>n;
for(int i=1; i<=n; i++){
cin>>a[i];
pos[a[i]] = i;
}
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
cnt[i][j] = cnt[i-1][j] + (a[i] >= j ?1 :0);
}
}
for(int i=1; i<=n; i++){
int sum = 0;
dp[i] = 1e9;
for(int j=i; j>=1; j--){
sum += cnt[pos[j]-1][i+1];
sum += (cnt[n][j] - cnt[n][i+1]) - (cnt[pos[j]][j] - cnt[pos[j]][i+1]);
dp[i] = min(dp[i], sum + dp[j-1]);
}
//cout<<dp[i]<<' ';
}
cout<<dp[n];
}
signed main(){
solve();
}
# | 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... |