NOIP 2003 plus two fork tree

Source: Internet
Author: User

Describe

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 I node is di,tree and each of its subtrees has an addition, and any subtrees tree subtree (which also contains 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 empty, it is specified to be divided into 1, and the addition of the leaves is the fraction of the leaf node itself. Without regard to its empty trees.

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

Format input Format

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 format

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.

Example 1 sample input 1[copy]
55 7 1) 2 10
Sample output 1[Copy]
1453 1 2) 4 5
Limit

1s per test point

  

1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cmath>5#include <algorithm>6#include <cstring>7 using namespacestd;8 intf[ +][ +];//the optimal solution between I and J, not strictly I to J, is likely to be greater in scope9 intg[ +][ +];//root between I and JTen inta[ the]; One intN; AInlinevoidDfsintLintR) { -     if(L>r)return;//No, not even a sample. -     if(l==R) { thecout<<l<<" "; -         return ; -     } -printf"%d", G[l][r]); +DFS (l,g[l][r]-1); -DFS (g[l][r]+1, R); + } A intMain () { atscanf"%d",&N); -      for(intI=1; i<=n;i++){ -scanf"%d",&a[i]); -f[i][i]=A[i]; -f[i][i-1]=1;//Focus, if the tree is a chain, and the root is used in the chain (no subtree) -     } inf[n+1][n]=1; -      for(intL=2; l<=n; l++) {//between L nodes to          for(intI=1; i<=n-l+1; i++) {//enumeration starting point +              for(intk=i;k<=i+l-1; k++) {//enumeration of root roots is possible at both ends -                 intj=i+l-1;//End the                 if(f[i][j]<f[i][k-1]*f[k+1][j]+A[k]) { *f[i][j]=f[i][k-1]*f[k+1][j]+A[k]; $g[i][j]=K; Panax Notoginseng                 } -             } the         } +     } Acout<<f[1][n]<<Endl; theDfs1, N);  +     return 0; -}

NOIP 2003 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.