zoukankan      html  css  js  c++  java
  • bzoj1564: [NOI2009]二叉查找树

    首先先说明一个坑点,这里说数不能重复,但是数又可以取全体实数,而且修改代价又和数没有关系,那么我们其实可以直接看成整数的。。。

    然后我们是知道中序遍历的:)

    所以我们可以区间DP一下,设f[i][j][k]表示i~j建成一棵子树,根的大小大于(否则值不变的时候只会影响1个点,答案没有单调性求值复杂,还要多一个n的复杂度)等于k的方案数

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    typedef long long LL;
    
    int n,K;
    struct node{int p,d,v;}a[110];int lslen,ls[110];LL sv[110];
    bool cmp(node n1,node n2){return n1.p<n2.p;}
    LL f[110][110][110];
    LL dfs(int l,int r,int d)
    {
        if(l>r)return 0;
        if(f[l][r][d]!=f[0][0][0])return f[l][r][d];
        for(int i=l;i<=r;i++)
        {
            f[l][r][d]=min(f[l][r][d],dfs(l,i-1,d)+dfs(i+1,r,d)+K);
            if(a[i].d>=d)f[l][r][d]=min(f[l][r][d],dfs(l,i-1,a[i].d+1)+dfs(i+1,r,a[i].d+1));
        }
        f[l][r][d]+=(sv[r]-sv[l-1]);
        return f[l][r][d];
    }
    
    int main()
    {
        scanf("%d%d",&n,&K);
        for(int i=1;i<=n;i++)scanf("%d",&a[i].p);
        for(int i=1;i<=n;i++)scanf("%d",&a[i].d);
        for(int i=1;i<=n;i++)scanf("%d",&a[i].v);
        sort(a+1,a+n+1,cmp);
        lslen=0;sv[0]=0;
        for(int i=1;i<=n;i++)
            ls[++lslen]=a[i].d,sv[i]=sv[i-1]+a[i].v;
        sort(ls+1,ls+lslen+1);
        lslen=unique(ls+1,ls+lslen+1)-ls-1;
        for(int i=1;i<=n;i++)
            a[i].d=lower_bound(ls+1,ls+lslen+1,a[i].d)-ls;
        
        memset(f,63,sizeof(f));
        printf("%lld
    ",dfs(1,n,1));
        return 0;
    }
  • 相关阅读:
    1定位与 2一些小标签的使用 3版心作用 4元素之间的转换 5项目准备
    H5 canvas建造敌人坦克
    H5 canvas控制坦克移动2
    H5 canvas控制坦克移动
    使用H5 canvas画一个坦克
    JSON
    inline-block元素间距问题的几种解决方案
    CSS垂直居中的11种实现方式
    30分钟误操作提示 您因长时间未进行操作导致页面过期
    多啦A梦css3
  • 原文地址:https://www.cnblogs.com/AKCqhzdy/p/9879317.html
Copyright © 2011-2022 走看看