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 main() {
int n;
cin>>n;
vector<int>h(n);
for(int i=0;i<n;i++){
cin>>h[i];
h[i]--;
}
vector<int>pos(n+1);
for(int i=0;i<n;i++){
pos[h[i]]=i;
}
vector<vector<int>>menor(n,vector<int>(n+1,0));
for(int j=n-1;j>0;j--){
for(int i=0;i<=h[j];i++){
menor[j-1][i]=menor[j][i]+1;
}
for(int i=h[j]+1;i<=n;i++){
menor[j-1][i]=menor[j][i];
}
}
vector<int>dp(n+1);
dp[0]=0;
for(int i=1;i<=n;i++){
dp[i]=INT_MAX;
int temp=0;
for(int j=i-1;j>=0;j--){
temp+=(n-i)+menor[pos[j]][j]-2*menor[pos[j]][i];
if((dp[j]+temp)<dp[i]){
dp[i]=(dp[j]+temp);
}
}
}
cout<<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... |