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 nx=5e3+5;
int dp[nx], n, x, idx[nx], cur;
struct fenwick
{
int d[nx];
void add(int i) {while (i<nx) d[i]++, i+=(i&-i);}
int query(int i)
{
int res=0;
while (i>0) res+=d[i], i-=(i&-i);
return res;
}
} f, l;
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n;
for (int i=1; i<=n; i++) cin>>x, idx[x]=i;
for (int i=1; i<=n; i++)
{
for (int j=1; j<=n; j++) l.d[j]=0;
dp[i]=INT_MAX;
cur=0;
f.add(idx[i]);
for (int j=i; j>0; j--)
{
cur+=f.query(nx-1)-f.query(idx[j]);
//if (i==4) cout<<"here1 "<<j<<' '<<cur<<'\n';
cur-=l.query(idx[j]);
l.add(idx[j]);
//if (i==4) cout<<"here2 "<<j<<' '<<cur<<'\n';
dp[i]=min(dp[i], dp[j-1]+cur);
}
//cout<<"debug "<<i<<' '<<dp[i]<<'\n';
}
cout<<dp[n];
}
# | 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... |