当前位置:网站首页>479. Additive binary tree (interval DP on the tree)
479. Additive binary tree (interval DP on the tree)
2022-07-02 01:45:00 【seez】
479. Add a binary tree - AcWing Question bank
analysis :
Conditions / nature :
- In the sequence traversal 1,2,3...n
- The left and right subtrees are empty , integral =1
- Integral of subtree = Left * Right + root
Traversal according to the middle order is 1,2,3...n, It can be divided into the following situations

Root node =k, The left section is all l, All the sections on the right are r, The left and right sections are independent of each other
that , It can be converted into an interval dp, The model of stone merging , seek Break point As Root node
Note that the subtree is empty , Special consideration is needed when the subtree is empty

State means :dp[i][j] Indicates that the traversal order of all middle orders is i~j Set
Recursive order :1<=len<=n ( Maybe it's just 1 Nodes )
initialization : All for 0
The border :dp[i][i]=w[i]
State calculation / subset division :
Because you need to enumerate [i~j] Who will be the root node in the set , There are three possible scenarios

- Only the left sub tree j As the root node , The left sub tree is 1
- Only the right subtree i As the root node , The right subtree is 1
- There are left and right subtrees enumeration [i+1,j-1] One of them k As the root node
Enumerate the root nodes according to the left and right subtrees of the current tree , It can be divided into three sets
- The left subtree dp[l][k-1] + Root node w[k] + Right subtree dp[k+1][j]
Depending on the break point , It can be divided into r-l+1 A subset of , Specifically =1 A rightless subtree +1 No left subtree + Multiple sets with left and right subtrees
For the result of preorder traversal , The former sequence traversal = Root left and right , So as long as Meiju chooses which one as the root node every time , That is to record what is chosen for each optimal decision
#include <iostream>
#include <algorithm>
using namespace std;
const int N=54;
int dp[N][N];
int w[N];
int g[N][N];
void dfs(int l,int r)
{
if(l>r) return ;
int p=g[l][r];
cout<<p<<' ';
dfs(l,p-1);
dfs(p+1,r);
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>w[i];
for(int len=1;len<=n;len++)
for(int i=1;i+len-1<=n;i++)
{
int j=i+len-1;
if(len==1)
dp[i][j]=w[i],g[i][j]=i; // By default, you use yourself as the root node
else
for(int k=i;k<=j;k++)
{
int left = k==i?1:dp[i][k-1];
int right = k ==j?1:dp[k+1][j];
int t=left*right+w[k];
if(t>dp[i][j])
{
dp[i][j]=t;
g[i][j]=k;
}
}
}
cout<<dp[1][n]<<endl;
dfs(1,n);
return 0;
}边栏推荐
- VARIATIONAL IMAGE COMPRESSION WITH A SCALE HYPERPRIOR文献实验复现
- Raspberry pie 4B learning notes - IO communication (1-wire)
- Study note 2 -- definition and value of high-precision map
- Electronic Society C language level 1 32, calculate the power of 2
- 大学的知识是否学而无用、过时?
- Using tabbar in wechat applet
- 现货黄金分析的技巧有什么呢?
- Convolutional neural network (including code and corresponding diagram)
- Laravel artisan 常用命令
- Should enterprises choose server free computing?
猜你喜欢

Learn C language from scratch day 025 (maze)

TSINGSEE青犀平台如何实现同一节点同时播放多个视频?

What are the skills of spot gold analysis?

Study note 2 -- definition and value of high-precision map

The technology boss is ready, and the topic of position C is up to you

Cross domain? Homology? Understand what is cross domain at once

Have you stepped on the nine common pits in the e-commerce system?

Learning note 24 - multi sensor post fusion technology
![[image enhancement] vascular image enhancement based on frangi filter with matlab code](/img/b3/b4164fb7db8645f470180e352b5717.png)
[image enhancement] vascular image enhancement based on frangi filter with matlab code
![[IVX junior engineer training course 10 papers to get certificates] 09 chat room production](/img/a8/25215e74162b7ab3f29df65681932b.jpg)
[IVX junior engineer training course 10 papers to get certificates] 09 chat room production
随机推荐
ECS project deployment
VARIATIONAL IMAGE COMPRESSION WITH A SCALE HYPERPRIOR文献实验复现
Docker installing Oracle_ 11g
Using tabbar in wechat applet
Ks006 student achievement management system based on SSM
Tencent cloud techo youth dream campus trip into Wuhan University
[IVX junior engineer training course 10 papers] 02 numerical binding and adaptive website production
It's already 30. Can you learn programming from scratch?
【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享
企业应该选择无服务器计算吗?
如何用一款产品推动「品牌的惊险一跃」?
uTools
KS006基于SSM实现学生成绩管理系统
电子协会 C语言 1级 33 、奇偶数判断
Liteos learning - first knowledge of development environment
Ubuntu20.04 PostgreSQL 14 installation configuration record
Fastadmin controls the length of fields in the table
Leetcode, 3 repeatless longest subsequence
C language 3-7 daffodils (enhanced version)
6-3漏洞利用-SSH环境搭建