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;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 5e3 + 5;
int n;
int h[N], pos[N], f[N][N], g[N][N], dp[N];
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> h[i];
pos[h[i]] = i;
}
for (int i = 1; i <= n; ++i) {
for (int j = n; j >= i; --j) {
f[i][j] = f[i][j + 1] + (pos[j] < pos[i]);
}
for (int j = i; j > 0; --j) {
g[j][i] = g[j + 1][i] + (pos[j] < pos[i]);
}
}
for (int i = 1; i <= n; ++i) {
for (int j = i - 2; j >= 1; --j) {
f[j][i] += f[j + 1][i];
}
for (int j = i + 2; j <= n; ++j) {
g[i][j] += g[i][j - 1];
}
}
memset(dp, 0x3f, sizeof(dp));
dp[0] = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < i; ++j) {
dp[i] = min(dp[i], dp[j] + f[j + 1][i + 1] + g[j + 1][i]);
}
}
cout << dp[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... |