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;
#define ll long long
#define f first
#define s second
#define x first
#define y second
#define elif else if
#define endl '\n'
#define all(x) begin(x), end(x)
int n,a[5069],pos[5069];
bool done[5069];
int cnt[5069][5069];
int dp[5069][5069];
int minimum=INT_MAX;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin>>n;
for (int i = 0; i < n; i++) {
cin>>a[i];
a[i]--;
pos[a[i]]=i;
cnt[i][0]=i-done[0];
for (int j = 1; j < n; j++) {
cnt[i][j]=cnt[i][j-1]-done[j];
}
done[a[i]]=true;
}
for (int i = n-1; i >= 0; i--) {
dp[i][i]=INT_MAX;
for (int j = i+1; j < n; j++) {
dp[i][i]=min(dp[i][i],dp[i+1][j]);
dp[i][j]=dp[i+1][j]+cnt[pos[i]][j]+j-i-(cnt[pos[i]][i]-cnt[pos[i]][j]);
}
if (dp[i][i]==INT_MAX) {
dp[i][i]=0;
}
dp[i][i]+=cnt[pos[i]][i];
}
for (int i = 0; i < n; i++) {
minimum=min(minimum,dp[0][i]);
}
cout<<minimum<<endl;
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... |