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 endl '\n'
#define st first
#define nd second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define ll long long
ll mod=1000000007;
int inf=1000000007;
ll infl=1000000000000000007;
int a[5007];
int ile[5007][5007];
int DP[5007];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
int inv=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<i;j++)
{
if(a[j]>a[i])
{
inv++;
ile[min(a[i],a[j])][max(a[i],a[j])]++;
}
}
}
for(int k=0;k<n;k++)
{
for(int i=1;i+k<=n;i++)
{
int j=i+k;
ile[i][j]+=ile[i+1][j]+ile[i][j-1]-ile[i+1][j-1];
}
}
for(int i=1;i<=n;i++)
{
DP[i]=inf;
for(int j=i-1;j>=0;j--)
{
DP[i]=min(DP[i],DP[j]+(i-j)*(i-j-1)/2-2*ile[j+1][i]);
}
}
cout<<inv+DP[n];
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... |