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 MX=5005;
int N;
int H[MX], dp[MX], cnt[MX][MX], curCnt[MX], pos[MX];
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
cin>>N;
for(int i=1;i<=N;i++) cin>>H[i];
for(int i=1;i<=N;i++) dp[i]=1e9;
for(int i=1;i<=N;i++) {
pos[H[i]]=i;
for(int j=H[i];j<=N;j++) curCnt[j]++;
for(int j=1;j<=N;j++) cnt[i][j]=curCnt[j];
}
for(int i=0;i<=N;i++) {
for(int k=i+1;k<=N;k++) {
int cost=0;
for(int j=k;j>i;j--) {
int cur=0;
cur+=cnt[pos[j]][N];
cur-=cnt[pos[j]][k];
cur+=cnt[pos[j]][j-1];
cur-=cnt[pos[j]][i];
cost+=cur;
}
dp[k]=min(dp[k],dp[i]+cost);
}
}
cout<<dp[N]<<'\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... |