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 n;
int a[5005];
int par[5005][5005];
int dp[5005];
int main() {
cin >> n;
for(int i=1; i<=n; i++) cin >> a[i];
int ans = 0;
for(int i=1; i<=n; i++) {
for(int j=i+1; j<=n; j++) {
if(a[i] > a[j]) {
par[a[j]][a[i]]++;
ans++;
}
}
}
for(int l=0; l<n; l++) {
for(int i=1; i<=n-l; i++) {
int j = i+l;
par[i][j] += par[i+1][j] + par[i][j-1] - par[i+1][j-1];
}
}
for(int i=1; i<=n; i++) {
dp[i] = -1e9;
for(int j=0; j<i; j++) {
dp[i] = max(dp[i], dp[j] + 2*par[j+1][i] - (i-j)*(i-j-1)/2);
}
}
cout << ans-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... |