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 x[5005][5005], b[5005], d[5005];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, a, s;
cin >> n;
for (int i=1; i<=n; i++)
{
cin >> a;
b[a]=i;
}
for (int i=1; i<=n; i++)
for (int j=1; j<=n; j++)
x[i][j]=x[i-1][j]+(j>=b[i]);
for (int i=1; i<=n; i++)
{
d[i]=1e9, s=0;
for (int j=i; j; j--)
{
s+=x[i][n]-x[i][b[j]]-x[j][n]+x[j][b[j]]+x[n][b[j]]-x[i][b[j]];
d[i]=min(d[i], d[j-1]+s);
}
}
cout << d[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... |