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>
//#pragma GCC optimize("fast-math")
using namespace std;
typedef long long ll;
int v[5005],n,dp[5005],nrinv[5005][5005],poz[5005],aib[5005],curpoz[5005];
int lsb(int x)
{
return x&(-x);
}
void update(int poz,int val)
{
for(int i=poz;i<=n;i+=lsb(i))
aib[i]+=val;
}
int suma(int poz)
{
int rez=0;
for(int i=poz;i>=1;i-=lsb(i))
rez+=aib[i];
return rez;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>v[i];
poz[v[i]]=i;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
aib[j]=0;
int inv=0;
for(int j=i;j<=n;j++)
{
int p=poz[j];
inv+=suma(p);
update(p,+1);
nrinv[i][j]=inv;
}
}
for(int k=n;k>=1;k--)
{
if(k==n)
{
dp[k]=0;
continue;
}
/*vector<int> p;
for(int i=1;i<=n;i++)
if(v[i]<=k)
p.push_back(v[i]);*/
for(int i=1;i<=n;i++)
aib[i]=0;
int lg=0;
for(int i=1;i<=n;i++)
if(v[i]>=k)
{
lg++;
curpoz[v[i]]=lg;
update(lg,+1);
}
dp[k]=2e9;
int need=0;
for(int i=n;i>=k;i--)
{
int val=need+nrinv[k][i]+dp[i+1];
dp[k]=min(dp[k],val);
int st=suma(curpoz[i]-1);
st=curpoz[i]-1-st;
need-=st;
int dr=suma(lg)-suma(curpoz[i]);
need+=dr;
update(curpoz[i],-1);
}
}
cout<<dp[1];
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... |