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;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n), pl(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
--a[i];
}
for (int i = 0; i < n; ++i) {
int cnt = 0;
for (int j = 0; j < n; ++j) {
if (a[j] >= i) {
++cnt;
}
if (a[j] == i) {
pl[i] = cnt;
break;
}
}
}
vector<vector<int>> dp(n, vector<int>(n + 1));
dp[n - 1][1] = 0;
for (int x = n - 2; x >= 0; --x) {
fill(dp[x].begin(), dp[x].end(), n * n);
for (int cnt = 1; cnt + 1 + x <= n; ++cnt) {
dp[x][1] = min(dp[x][1], dp[x + 1][cnt] + abs(pl[x] - 1));
}
for (int cnt = 2; x + cnt <= n; ++cnt) {
dp[x][cnt] = dp[x + 1][cnt - 1] + abs(pl[x] - cnt);
}
}
int ans = n * n;
for (int i = 1; i <= n; ++i) {
ans = min(ans, dp[0][i]);
}
cout << ans;
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... |