【bzoj5125】[Lydsy12月赛]小Q的书架 dp优化

【bzoj5125】[Lydsy12月赛]小Q的书架 dp优化_第1张图片

题解
http://www.lydsy.com/JudgeOnline/upload/201712/sol12.pdf
(我只是做个记录。。)

代码

#include
#define ll long long
#define inf 1000000000
#define mod 65537
using namespace std;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,t[40005],L=1,R=0,ans,a[40005],dp[40005],DP[40005],k;
inline void add(int x,int y)
{
    for (;x<=n;x+=x&-x)t[x]+=y;
}
inline int query(int x)
{
    int ans=0;
    for (;x;x-=x&-x)ans+=t[x];
    return ans;
}
inline void Move(int l,int r)
{
    while (L1);ans-=query(a[L]-1);L++;}
    while (L>l){L--;add(a[L],1);ans+=query(a[L]-1);}
    while (R1);ans+=(R-L+1)-query(a[R]);}
    while (R>r){add(a[R],-1);ans-=(R-L)-query(a[R]);R--;}
}
void solve(int l,int r,int L,int R)
{
    if (l>r) return;
    int mid=(l+r)>>1,MIN=1600000005,p=L;
    for (int i=L;i<=R&&i1,mid);
        if (dp[i]+ans1,L,p);
    solve(mid+1,r,p,R);
}
int main()
{
    n=read();k=read();for (int i=1;i<=n;i++) a[i]=read();
    for (int i=1;i<=n;i++)
    {
        Move(1,i);dp[i]=ans;
    }
    for (int i=1;i1,n,1,n);
        for (int i=1;i<=n;i++) dp[i]=DP[i];
    }
    printf("%d",dp[n]);
    return 0;
}

你可能感兴趣的:(dp)