#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define len(v) (int)((v).size())
const int inf = 1e9;
const int sz = 1 << 20;
inline void solve(){
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i){
cin >> a[i];
--a[i];
}
vector<ll> dp(n + 1, inf);
dp[0] = 0;
for (int i = 0; i < n; ++i){
vector<int> p;
for (int j = 0; j < n; ++j){
if (a[j] >= i){
p.push_back(a[j]);
}
}
vector<int> q(n);
for (int j = 0; j < n - i; ++j){
q[p[j]] = j;
}
ll res = 0;
for (int j = i; j < n; ++j){
res += q[j];
for (int f = i; f < j; ++f){
res -= (q[f] > q[j]);
}
dp[j + 1] = min(dp[j + 1], dp[i] + res);
}
}
cout << dp[n] << '\n';
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cout.precision(60);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}
# | 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... |