This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#ifdef LOCAL
# define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#include <immintrin.h>
#ifndef LOCAL
// #pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("avx,avx2,bmi,bmi2,popcnt,lzcnt,sse,sse2,sse3,ssse3,mmx")
#endif
using namespace std;
using ll = long long;
using ld = long double;
# define x first
# define y second
# define all(x) x.begin(), x.end()
# define rall(x) x.rbegin(), x.rend()
mt19937_64 mt(time(0));
void solve();
void init();
int32_t main() {
#ifndef LOCAL
cin.tie(nullptr)->sync_with_stdio(false);
#endif
cout << fixed << setprecision(30);
init();
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}
void init() {}
void solve() {
int n;
cin >> n;
vector<pair<int, int>> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i].x;
a[i].y = i;
}
sort(all(a), greater<>());
vector<vector<int>> dp1(n, vector<int>(n)), dp2(n, vector<int>(n));
for (int i = 0; i < n; i++) {
dp2[i][i] = 0;
for (int j = i - 1; j >= 0; j--) {
if (a[j].y > a[i].y) {
dp2[i][j] = dp2[i][j + 1] + 1;
} else {
dp2[i][j] = dp2[i][j + 1];
}
}
}
for (int i = 0; i < n; i++) {
dp1[i][i] = 0;
for (int j = i + 1; j < n; j++) {
dp1[i][j] = dp1[i][j - 1] + dp2[j][i];
}
}
int off = n * (n - 1) / 2 - dp1[0][n - 1];
vector<int> dp(n + 1, 0);
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
dp1[i][j] = 2 * dp1[i][j] - (j - i) * (j - i + 1) / 2;
}
}
for (int i = 1; i <= n; i++) {
for (int j = i - 1; j >= 0; j--) {
dp[i] = min(dp[i], dp[j] + dp1[j][i - 1]);
}
}
cout << off + 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... |