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;
int n, a[5001], dp[5001], pos[5001], inv[5001][5001], calc2[5001][5001];
struct BIT {
int tree[5001];
void add (int x) {
for (; x <= 5000; x += x & (-x)) tree[x]++;
}
int get (int x) {
int sum = 0;
for (; x > 0; x -= x & (-x)) sum += tree[x];
return sum;
}
} cur;
int main () {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i], pos[a[i]] = i;
for (int i = 1; i <= n; i++) {
memset(cur.tree, 0, sizeof(cur.tree));
for (int j = i; j <= n; j++) {
inv[i][j] = cur.get(pos[j] - 1) + inv[i][j - 1];
cur.add(pos[j]);
}
}
for (int i = 1; i <= n; i++) {
for (int j = i - 1; j >= 1; j--) {
if (a[j] > a[i]) {
calc2[a[i] + 1][a[j]]++;
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
calc2[j][i] += calc2[j - 1][i];
}
}
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j++) {
calc2[i][j] += calc2[i][j - 1];
}
}
for (int i = 1; i <= n; i++) {
dp[i] = 1e9;
for (int j = i; j >= 1; j--) {
int sum = calc2[j][i] - calc2[j][j - 1];
dp[i] = min(dp[i], dp[j - 1] + sum + inv[j][i]);
}
}
cout << dp[n] << '\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... |