이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |