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 oo = 1 << 30;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<int> id(n + 1);
for (int i = 0; i < n; i++)
{
int x;
cin >> x;
id[x] = i;
}
vector<vector<int>> c(n + 1, vector<int>(n + 1));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
{
c[i][j] = c[i - 1][j] + c[i][j - 1] - c[i - 1][j - 1];
if (j < i && id[j] < id[i])
c[i][j]++;
}
vector<int> f(n + 1, oo);
f[0] = 0;
for (int i = 1; i <= n; i++)
for (int j = 0; j < i; j++)
{
int cost = c[i][i] + c[j][j] - c[i][j] - c[j][i];
cost += (n - i) * (i - j) - (c[n][i] + c[i][j] - c[n][j] - c[i][i]);
f[i] = min(f[i], f[j] + cost);
}
cout << f[n] << 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... |