bzoj1564: [NOI2009]二叉查找树

#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\n",dfs(1,n,1));
return 0;
}

pain and happy in the cruel world.
posted @ 2018-10-30 20:25  AKCqhzdy  阅读(96)  评论(0编辑  收藏  举报