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 int long long
const int MAX_N = 5001;
int n;
int dp[MAX_N];
int vals[MAX_N];
int pos[MAX_N];
int oc_under_pref[MAX_N][MAX_N];
void precalc(){
for(int i = 0; i<=n; i++){
vector<int> oc(n, 0);
for(int j = 0; j<i; j++){
oc[vals[j]]++;
}
int cur = 0;
for(int j=0; j<=n; j++){
oc_under_pref[i][j] = cur;
if(j<n){
cur += oc[j];
}
}
}
}
int find_dp(){
dp[0] = 0;
for(int i = 0; i<n; i++){
int cur_c= 0;
for(int j = i; j<n; j++){
//cout<<"j "<<j<<" "<<pos[j]<<" "<<oc_under_pref[pos[j]][j]<<endl;
cur_c += pos[j]-oc_under_pref[pos[j]][i];
cur_c-= (oc_under_pref[n][j]-oc_under_pref[n][i]-oc_under_pref[pos[j]][j]+oc_under_pref[pos[j]][i]);
dp[j+1] =min(dp[j+1], dp[i]+cur_c);
}
}
return dp[n];
}
signed main(){
cin>>n;
for(int i = 0; i<n; i++){
cin>>vals[i];
vals[i]--;
pos[vals[i]] = i;
}
for(int i = 0; i<=n; i++){
dp[i] = 1e18;
}
precalc();
cout<<find_dp()<<endl;
}
# | 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... |