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 main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> h(n);
for (int i = 0; i < n; i++) {
cin >> h[i];
}
vector<int> pos(n + 1);
for (int i = 0; i < n; i++) {
pos[h[i]] = i;
}
const int inf = 1e9;
vector<int> dp(n + 1, inf);
dp[0] = 0;
auto Solve = [&](int L, int R) {
int res = 0;
for (int i = L; i <= R; i++) {
for (int j = 1; j < L; j++) {
if (pos[i] < pos[j]) {
res += 1;
}
}
}
for (int i = L; i <= R; i++) {
for (int j = L; j < i; j++) {
if (pos[i] > pos[j]) {
res += 1;
}
}
}
return res;
};
for (int i = 0; i < n; i++) {
for (int j = i + 1; j <= n; j++) {
dp[j] = min(dp[j], dp[i] + Solve(i + 1, j));
}
}
cout << dp[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... |