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 = 5005;
const int INF = 1e9;
int n;
int H[MAXN], dp[MAXN], pos[MAXN];
int f[MAXN][MAXN];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++)
{
dp[i] = INF;
cin >> H[i];
pos[H[i]] = i;
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j < i; j++)
{
f[i][j] = f[i][j - 1] + (pos[i] < pos[j]);
}
}
for(int i = 0; i <= n; i++)
{
int Cost = 0, Inv = 0;
for(int j = i + 1; j <= n; j++)
{
Cost += f[j][i];
Inv += (f[j][j - 1] - f[j][i]);
dp[j] = min(dp[j], dp[i] + Cost + min(Inv, (j - i) * (j - i - 1) / 2 - Inv));
}
}
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... |