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 MAX_N = 5000;
int h[MAX_N + 1], p[MAX_N + 1], lower[MAX_N + 1][MAX_N + 1], costSmall[MAX_N + 1][MAX_N + 1], costBig[MAX_N + 1][MAX_N + 1], minCost[MAX_N + 1];
int main() {
int n;
cin >> n;
for ( int i = 1; i <= n; i++ ) {
cin >> h[i];
p[h[i]] = i;
}
for ( int i = 1; i <= n; i++ ) {
for ( int j = 1; j < i; j++ )
lower[i][h[j]]++;
for ( int j = 1; j <= n; j++ )
lower[i][j] += lower[i][j - 1];
}
for ( int l = 1; l <= n; l++ ) {
for ( int r = l; r <= n; r++ )
costSmall[l][r] = costSmall[l][r - 1] + lower[p[r]][r - 1] - lower[p[r]][l - 1];
}
for ( int r = 1; r <= n; r++ ) {
for ( int l = r; l >= 1; l--)
costBig[l][r] = costBig[l + 1][r] + lower[p[l]][n] - lower[p[l]][r];
}
for ( int i = 1; i <= n; i++ ) {
minCost[i] = n * n;
for ( int j = 0; j < i; j++ )
minCost[i] = min( minCost[i], minCost[j] + costSmall[j + 1][i] + costBig[j + 1][i] );
}
cout << minCost[n] << "\n";
return 0;
}
# | 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... |