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>
#define TASKNAME "codeforce"
#define pb push_back
#define pli pair<int,int>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxN=2e5;
const ll inf=1e18;
const ll mod=1e9+7;
ll n,bit[3][maxN];
void update(ll t,ll i,ll v)
{
while(i<=n)
{
bit[t][i]+=v;
i+=(i&(-i));
}
}
ll get(ll t,ll i)
{
ll ans=0;
while(i>0)
{
ans+=bit[t][i];
i-=(i&(-i));
}
return ans;
}
ll a[maxN],pos[maxN],dp[maxN];
void solve()
{
cin >> n;
for(int i=1;i<=n;i++) cin >> a[i] ,pos[a[i]]=i;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=2;j++) fill(bit[j],bit[j]+n+1,0);
ll sum1=0,sum2=0;
for(int j=1;j<=i;j++) update(2,pos[j],1);
dp[i]=inf;
for(int j=i;j>=1;j--)
{
sum1+=get(1,n)-get(1,pos[j]);
update(2,pos[j],-1);
sum2+=get(2,n)-get(2,pos[j]);
sum2-=get(1,pos[j]);
update(1,pos[j],1);
dp[i]=min(dp[i],dp[j-1]+sum1+sum2);
}
}
cout << dp[n];
}
int main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
# | 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... |