# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
807716 | dong_liu | Group Photo (JOI21_ho_t3) | C++17 | 309 ms | 98324 KiB |
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 <iostream>
using namespace std;
const int N = 5e3;
int main() {
int n;
scanf("%d", &n);
static int o[N + 1];
for (int i = 1; i <= n; i++) {
int a;
scanf("%d", &a);
o[a] = i;
}
static int s[N + 1][N + 1];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
s[i][j] =
s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + (i < j && o[i] > o[j]);
auto inv = [&](int i1, int i2, int j1, int j2) {
return s[i2][j2] - s[i1 - 1][j2] - s[i2][j1 - 1] + s[i1 - 1][j1 - 1];
};
auto ninv = [&](int i, int j) {
return (j - i + 1) * (j - i) / 2 - inv(i, j, i, j);
};
static int f[N + 1];
for (int i = 1; i <= n; i++) {
f[i] = ninv(1, i);
for (int j = 1; j < i; j++)
f[i] = min(f[i], f[j] + inv(1, j, j + 1, i) + ninv(j + 1, i));
}
printf("%d\n", f[n]);
return 0;
}
Compilation message (stderr)
# | 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... |