#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) {
dp[x][1] = 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 |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
320 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
320 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
320 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
320 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
320 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |