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 maxn = 5000;
int utan[maxn][maxn], ert[maxn][maxn];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;
vector<int> v(n), inv(n);
for(int i = 0; i < n; i++){
cin>>v[i];
--v[i];
inv[v[i]] = i;
}
for(int i = 0; i < n; i++)
for(int j = i-1; j >= 0; j--)
utan[i][j] = utan[i][j+1] + (inv[i] < inv[j] ? 1 : 0);
vector<int> db(n);
iota(db.begin(), db.end(), 0);
for(int i = 0; i < n; i++){
ert[i][i] = db[inv[i]];
for(int j = i+1; j < n; j++)
ert[i][j] = ert[i][j-1] + db[inv[j]] - utan[j][i];
for(int j = inv[i]+1; j < n; j++)
db[j]--;
}
/*cout<<"ert:\n";
for(int i = 0; i < n; i++){
for(int j = i; j < n; j++)
cout<<ert[i][j]<<' ';
cout<<'\n';
}*/
vector<int> dp(n+1, (int)1e9);
dp[0] = 0;
for(int i = 1; i <= n; i++)
for(int j = i; j > 0; j--)
dp[i] = min(dp[i], dp[j-1] + ert[j-1][i-1]);
cout<<dp[n]<<'\n';
return 0;
}
/*
5
3 5 2 4 1
*/
# | 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... |