1090 Plus two fork tree

Source: Internet
Author: User

1090 plus two fork tree

2003 NOIP National League Improvement Group

time limit: 1 sspace limit: 128000 KBtitle level: Diamonds Diamond SolvingTitle Description 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

Now, please help your good friend XZ design a program to get the right answer.

Enter a description Input Description

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 description Output Description

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 Sample Input

5

5 7 1) 2 10

Sample output Sample Output

145

3 1 2) 4 5

Data range and Tips Data Size & Hint

N (n<=30)

Score <=100

Category labels Tags Click here to expandDynamic planning interval DP Continental Region NOIP National League increase Group 2003
/*The tree has no effect---tree DP; because it satisfies the sequence traversal, the node number of a subtree must be within a continuous interval; set Dp[l][r] indicates the maximum addition of the interval [l,r], c[l][r] is what combination of maximum plus ticks; boundary: Dp[l][r]=1 (l >R), Dp[l][r]=w[l] (l=r) equation: Dp[l][r]=max (dp[l][i-1]+dp[i+1][r]+w[i]) The result is dp[1][n], and get C directly can build, 2 asked all solved;*/#include<iostream>#include<cstdio>#include<cstring>using namespacestd;Long Longdp[ *][ *],n,w[ *],c[ *][ *];voidFindLong LongLeftLong LongRight ) {    if(Left>right)return; printf ("%d", C[left][right]); if(left!=Right ) {Find (Left,c[left][right]-1); Find (C[left][right]+1, right); }    return;}intMain () {scanf ("%lld",&N);  for(Long LongI=1; i<=n;i++) scanf ("%lld",&W[i]);  for(Long LongI=1; i<=n;i++) {Dp[i][i]=W[i]; Dp[i][i-1]=1; C[i][i]=i; }     for(Long Longj=1; j<=n;j++)         for(Long Longi=j-1; i>=1; i--)             for(intk=i;k<=j;k++){                if(dp[i][j]<dp[i][k-1]*dp[k+1][j]+W[k]) {Dp[i][j]=dp[i][k-1]*dp[k+1][j]+W[k]; C[I][J]=K; }} printf ("%lld\n", dp[1][n]); Find (1, N); return 0;}

1090 Plus two fork tree

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.