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;
const int maxn = 5001;
short lsf[maxn][maxn];
int otr[maxn][maxn];
int dp[maxn];
int h[maxn];
int ih[maxn];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for(int i = 0;i < n;++i)
{
cin >> h[i];
h[i]--;
ih[h[i]] = i;
}
for(int i = 0;i < n;++i)
{
for(int j = n-1;j >= 0;--j)
{
lsf[i][j] = (j == n-1 ? ih[i] > ih[j] : (ih[i] > ih[j]) + lsf[i][j+1]);
}
}
for(int l = 0;l < n;++l)
{
for(int r = l;r < n;++r)
{
otr[l][r] = (l == r ? lsf[l][l] : lsf[r][l]*2 - lsf[r][r] - r + l + otr[l][r-1]);
}
}
dp[n] = 0;
for(int i = n-1;i >= 0;--i)
{
dp[i] = 1e9;
for(int j = i+1;j <= n;++j)
{
dp[i] = min(dp[i],dp[j] + otr[i][j-1]);
}
}
cout << dp[0];
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... |