NOIP-2003 plus two forks of trees

Source: Internet
Author: User

Title Description

The middle order traversal of a two-ary tree with an n node is (,..., 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:

The subtree of the left subtree of the Xsubtree is added to the right sub-tree to add the score of the +subtree root.

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

                                ———————— by Rokua OJ

http://www.luogu.org/problem/show?pid=1040

《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《

Obviously a tree DP but unlike the mainstream tree DP, the specific tree in the subject is not a condition, but a request. But in the title still give the tree of the middle sequence traversal (otherwise how to do), so think of the nature of the sequence traversal, point A of the Zuozi in a left, right subtree in a right, and then analysis of test instructions, obviously for each root node A, the addition of his tree in the determination of the son and son subtree of the optimal addition points can be determined, the latter is implemented by DP, The former needs to be enumerated in two alternate interval, and then the state transfer equation is obtained.

    • F[fa][l][r]=a[fa]+max (F[i][l][fa-1]*f[j][fa+1][r]) (I,J) ∈{(A, B) |a∈[l,fa-1],b∈[fa+1,r]}

The rest is a few individual treatments, such as when fa-1=l or fa+1=r are specially handled.

So the first task of the subject was completed, leaving the building of a tree. This is also very simple-for ls[fa][l][r] and Rs[fa][l][r],f[fa][l][r] transferred from I,j, so ls[fa][l][r]=i,rs[fa][l][r]=j, at the same time as DP.

The code is as follows:

#include <cstdio>using namespacestd;intN;inta[ to];Long Longf[ to][ to][ to];intls[ to][ to][ to],rs[ to][ to][ to];Long Longdpint,int,int);voidDlrint,int,int);intMain () {intI,fa; Long Longnum=0, ans=0; scanf ("%d",&N);  for(i=1; i<=n;i++) scanf ("%d",&A[i]);  for(i=1; i<=n;i++) {num=DP (1, N,i); if(ans<num) {ans=num; FA=i; }} printf ("%lld\n", ans); DLR (1, N,FA);}Long LongdpintLintRintFA) {    inti,j; Long Longans; if(f[fa][l][r]!=0)        returnF[fa][l][r]; F[FA][L][R]=A[FA];  for(i=l;i<fa;i++)    {         for(j=fa+1; j<=r;j++) {ans=A[FA]+DP (l,fa-1, i) *DP (fa+1, r,j); if(f[fa][l][r]<ans) {F[fa][l][r]=ans; LS[FA][L][R]=i; RS[FA][L][R]=J; }        }    }    if(l==FA) {         for(j=fa+1; j<=r;j++) {ans=A[FA]+DP (fa+1, r,j); if(f[fa][l][r]<ans) {F[fa][l][r]=ans; RS[FA][L][R]=J; }        }    }    if(r==FA) {         for(i=l;i<fa;i++) {ans=A[FA]+DP (l,fa-1, i); if(f[fa][l][r]<ans) {F[fa][l][r]=ans; LS[FA][L][R]=i; }        }    }    returnf[fa][l][r];}voidDlrintLintRintFA) {printf ("%d", FA); if(Ls[fa][l][r]) DLR (L,FA-1, Ls[fa][l][r]); if(Rs[fa][l][r]) DLR (FA+1, R,rs[fa][l][r]);}

Wish AC

NOIP-2003 plus two forks of trees

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.