bzoj1564: [NOI2009]二叉查找树
生活随笔
收集整理的這篇文章主要介紹了
bzoj1564: [NOI2009]二叉查找树
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
首先先說明一個坑點,這里說數(shù)不能重復(fù),但是數(shù)又可以取全體實數(shù),而且修改代價又和數(shù)沒有關(guān)系,那么我們其實可以直接看成整數(shù)的。。。
然后我們是知道中序遍歷的:)
所以我們可以區(qū)間DP一下,設(shè)f[i][j][k]表示i~j建成一棵子樹,根的大小大于(否則值不變的時候只會影響1個點,答案沒有單調(diào)性求值復(fù)雜,還要多一個n的復(fù)雜度)等于k的方案數(shù)
#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; }?
轉(zhuǎn)載于:https://www.cnblogs.com/AKCqhzdy/p/9879317.html
總結(jié)
以上是生活随笔為你收集整理的bzoj1564: [NOI2009]二叉查找树的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JVM内存GC的骗局
- 下一篇: nginx引用外部配置