#include <iostream>
#include <vector>
using namespace std;
int n, t[5005], a[5005], c[5005], f[5005];
void upd(int x) {
for (int i = x; i <= n; i += i & (-i)) {
t[i]++;
}
}
int sum(int x) {
int res = 0;
for (int i = x; i; i -= i & (-i)) {
res += t[i];
}
return res;
}
int sum(int l, int r) {
return sum(r) - sum(l - 1);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
f[i] = 1e9;
}
for (int i = 0; i < n; i++) {
vector<int> v;
for (int j = 1; j <= n; j++) {
if (a[j] > i) v.push_back(a[j]);
c[j] = t[j] = 0;
}
for (int j = 0; j < v.size(); j++) c[v[j]] += j + 1;
for (int j = i + 1; j <= n; j++) {
c[j] += c[j - 1];
}
int d = 0;
for (int j = i + 1; j <= n; j++) {
int h = c[j] - (j - i + 1) * (j - i) / 2;
d += sum(1, c[j] - c[j - 1] - 1);
f[j] = min(f[j], f[i] + h + d);
upd(c[j] - c[j - 1]);
}
}
cout << f[n];
}
# | 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... |