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;
inline void fastio() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
}
ll n, arr[5050], p[5050], dp[5050], sum[5050][5050];
int main() {
fastio();
cin >> n;
for (ll i = 1; i <= n; i++) {
cin >> arr[i];
p[arr[i]] = i;
}
for (ll i = 1; i <= n; i++) {
for (ll j = 1; j <= n; j++) {
sum[i][j] += sum[i - 1][j] + (arr[i] <= j);
}
}
memset(dp, 0x3f, sizeof(dp));
*dp = 0;
for (ll i = 1; i <= n; i++) {
for (ll j = i, tmp = 0; j > 0; j--) {
tmp += (i - j) + (sum[p[j]][n] - sum[p[j]][i]) - (sum[p[j]][i] - sum[p[j]][j]);
dp[i] = min(dp[i], dp[j - 1] + tmp);
}
}
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... |