Add two fork Tree _ Problem Solving report _ssl1033_2003 Year Division Three _ tree DP

Source: Internet
Author: User

Description

The mid-order traversal of a two-ary tree with an n node is (l,2,3,..., N), where the number,..., n is a node number. Each node has a fraction (all positive integers), the score for the J node is di,tree and each of its subtrees has an extra point, and any subtrees tree subtree (also including the tree itself) is calculated as follows:

Subtree The xsubtree of the right subtree of the left sub-tree and the score of the root of the +subtree.

If a subtree is dominated, it is specified to be divided into 1, and the addition of leaves is the fraction of the leaf node itself. Without regard to its emptiness

Subtree

Try to find a tree of two trees that matches the middle order traversal (,..., N) and has the highest added score. Required output;

(1) Maximum bonus points for tree

(2) The tree's pre-sequence traversal

Input

Line 1th: An integer n (n<30), which is the number of nodes.

Line 2nd: n spaces separated by integers, for each node's score (fractional <100).

Output

Line 1th: An integer that is the highest plus score (the result will not exceed 4,000,000,000).

Line 2nd: N A space-separated integer, for the tree's pre-order traversal.

Sample Input

5

5 7 1) 2 10

Sample Output

145

3 1 2) 4 5

Ideas:

Obviously, the subject is suitable for solving with dynamic programming. If you use array value[i,j] to represent the maximum addition of a two-fork tree from node I to node J, the dynamic equation can be expressed as follows:

value[i,j]=max{value[i,i]+value[i+1,j],value[i+1,i+1]+value[i,i]*value[i+2,j],value[i+2,i+2]+value[i,i+1]* VALUE[I+3,J],..., Value[j-1,j-1]+value[i,j-2]*value[j,j],value[j,j]+value[i,j-1]}

The topic also requires the output of the maximum addition of the sequence of sequential traversal of the tree, so it must be in the calculation process from node I to node J of the maximum number of two fork tree root node, with an array of root[i,j] representation. In the actual test, however, I don't know why I don't have to go through the sequence of output sequences, I wa all night.

Source Code/pas:

var  n:longint;  F,r:array[1..30,1..30]of Longint;  A:array[1..30]of longint;procedure init;var  i:longint;begin  readln (n);  For I:=1 to n do  read (A[i]);  For I:=1 to N-1 do  begin    F[i,i]:=a[i];    r[i,i]:=i;    F[I,I+1]:=A[I]+A[I+1];    r[i,i+1]:=i;  End;  F[n,n]:=a[n];  R[n,n]:=n;end;procedure Main;var  i,j,k:longint;  Max:longint;begin for k:=2 to N-1 does for  i:=1 to N-k do  begin    Max:=f[i,i]+f[i+1,i+k];    r[i,i+k]:=i;    For J:=1 to K does    begin      if F[i+j,i+j]+f[i,i+j-1]*f[i+j+1,i+k]>max then      begin        Max:=f[i+j,i+j]+f [I,i+j-1]*f[i+j+1,i+k];        R[i,i+k]:=i+j;      End;    End;    If F[i+k,i+k]+f[i,i+k-1]>max then    begin      Max:=f[i+k,i+k]+f[i,i+k-1];      R[i,i+k]:=i+j;    End;    F[i,i+k]:=max;  End;  Writeln (F[1,n]); End;begin  init;  Main;end.

Add two fork Tree _ Problem Solving report _ssl1033_2003 Year Division Three _ tree DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.