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[5005],a[5005],pos[5005],n,ans,ps[5005][5005];
int main(){
ios::sync_with_stdio(false);
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=i+1;j<=n;j++){
if (pos[i] > pos[j])
ans++;
}
}
for (int i=2;i<=n;i++){
for (int j=1;j+i-1<=n;j++){
ps[i][j]=ps[i-1][j]+ps[i-1][j+1]-ps[i-2][j+1];
if (pos[j] > pos[j+i-1])
ps[i][j]++;
}
}
for (int i=1;i<=n;i++){
for (int j=1;j<=i;j++){
dp[i]=min(dp[i],dp[j-1]-(ps[i-j+1][j]-(i-j+1)*(i-j)/2+ps[i-j+1][j]));
}
}
cout << ans+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... |