当前位置:网站首页>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;
}边栏推荐
- This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
- Matlab uses audiorecorder and recordblocking to record sound, play to play sound, and audiobook to save sound
- MATLAB realizes voice signal resampling and normalization, and plays the comparison effect
- How can I batch produce the same title for the video?
- What is AQS and its principle
- [IVX junior engineer training course 10 papers] 04 canvas and a group photo of IVX and me
- np.where 和 torch.where 用法
- Develop those things: how to use go singleton mode to ensure the security of high concurrency of streaming media?
- 5g/4g pole gateway_ Smart pole gateway
- 1217 supermarket coin processor
猜你喜欢

Discussion on the idea of platform construction
![[IVX junior engineer training course 10 papers] 05 canvas and aircraft war game production](/img/dc/e9adb1b41c2175c6f18d8027e0530a.jpg)
[IVX junior engineer training course 10 papers] 05 canvas and aircraft war game production

浅浅了解Servlet

Using tabbar in wechat applet

Develop those things: how to use go singleton mode to ensure the security of high concurrency of streaming media?

现货黄金分析的技巧有什么呢?

Réseau neuronal convolutif (y compris le Code et l'illustration correspondante)

How can the tsingsee Qingxi platform play multiple videos at the same time on the same node?

如何远程、在线调试app?

Should enterprises choose server free computing?
随机推荐
Makefile simple induction
Introduction to ffmpeg Lib
With the innovation and upgrading of development tools, Kunpeng promotes the "bamboo forest" growth of the computing industry
[IVX junior engineer training course 10 papers to get certificates] 01 learn about IVX and complete the New Year greeting card
遊戲思考15:全區全服和分區分服的思考
uTools
SAP ui5 beginner tutorial 20 - explanation of expression binding usage of SAP ui5
Edge computing accelerates live video scenes: clearer, smoother, and more real-time
Volume compression, decompression
Three core problems of concurrent programming
自动浏览拼多多商品
[Obsidian] wechat is sent to Obsidian using remotely save S3 compatibility
Bat Android Engineer interview process analysis + restore the most authentic and complete first-line company interview questions
Learn basic K-line diagram knowledge in three minutes
ES6 new method of string
What are the skills of spot gold analysis?
[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing
Learning note 24 - multi sensor post fusion technology
2022年6月国产数据库大事记
1218 square or round