(2)tree的前序遍历
分析:很裸的dp。f[i,j]表示节点i到节点j的最高加分是多少。
很显然f[i,j]=max(d[i]+f[i+1,j],d[j]+f[i,j-1],f[i,k-1]*f[k+1,j]+d[k]){i+1<=k<=j-1}
代码:
var n,i,j,k,l:longint; a:array[1..30] of longint; f:array[1..30,1..30] of int64; g:array[1..30,1..30] of longint; procedure print(x,y:longint); begin if x>y then exit; write(g[x,y]:3); print(x,g[x,y]-1); print(g[x,y]+1,y); end; begin readln(n); for i:=1 to n do read(a[i]); for i:=1 to n do begin f[i,i]:=a[i]; g[i,i]:=i; end; for l:=1 to n-1 do for i:=1 to n-l do begin j:=i+l; f[i,j]:=f[i,i]+f[i+1,j]; g[i,j]:=i; if f[j,j]+f[i,j-1]>f[i,j] then begin f[i,j]:=f[j,j]+f[i,j-1]; g[i,j]:=j; end; for k:=i+1 to j-1 do if f[i,k-1]*f[k+1,j]+f[k,k]>f[i,j] then begin f[i,j]:=f[i,k-1]*f[k+1,j]+f[k,k]; g[i,j]:=k; end; end; writeln(f[1,n]); print(1,n); end.