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 a[5005], pos[5005], dp[5005], ulta[5005][5005];
signed main(){
int n; cin >> n;
for(int i = 1; i < n + 1; i++){
cin >> a[i];
pos[a[i]] = i;
}
for(int i = 2; i < n + 1; i++){
int cnt = 0;
for(int j = i - 1; j > 0; j--){
if(pos[j] > pos[i]) cnt++;
ulta[j][i] = ulta[j][i - 1] + cnt;
}
}
for(int i = 1; i < n + 1; i++){
dp[i] = INT_MAX;
for(int j = 0; j < i; j++){
dp[i] = min(dp[i], dp[j] + ulta[1][i] - ulta[1][j] + ((i - j) * (i - j - 1)) / 2 - 2 * ulta[j + 1][i]);
}
}
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... |