// JOI 2021 (Group Photo)
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define fi first
#define se second
#define ins insert
#define eb emplace_back
ll n;
const int mxn = 5001;
ll it[mxn];
ll dp[mxn];
ll ind[mxn];
ll cost[mxn][mxn];
ll pre[mxn][mxn];
void sol()
{
cin >> n;
for(ll i=0;i<n;i++){
cin >> it[i];
ind[it[i]] = i;
}
for(ll i=1;i<=n;i++){
for(ll j=1;j<=n;j++){
pre[i][j] = pre[i][j-1];
if(ind[j] > ind[i]){
pre[i][j]++;
}
}
}
for(ll i=1;i<=n;i++){
for(ll j=i;j<=n;j++){
cost[i][j] = 0;
if(i < j){
cost[i][j] += cost[i][j-1];
}
cost[i][j] += pre[j][i-1];
if(i < j){
cost[i][j] += (j - i) - (pre[j][j-1] - pre[j][i-1]);
}
}
}
for(ll i=0;i<=n;i++){
dp[i] = 1e9;
}
dp[0] = 0;
for(ll i=1;i<=n;i++){
for(ll j=1;j<=i;j++){
dp[i] = min(dp[i], dp[j-1] + cost[j][i]);
}
}
cout << dp[n] << '\n';
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
sol();
return 0;
}
# | 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... |