NOIP 2003 加分二叉树
来源:互联网 发布:淘宝外观专利权投诉 编辑:程序博客网 时间:2024/05/20 19:50
评测传送
这个题可以记忆化搜索做的。
注意边界条件。
对于先序遍历,递归就可以了(在dfs的时候用一个数组记录根节点就可以了)。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<ctime>#include<queue>#define LL long longusing namespace std;int n;LL a[50],ans,sum;int way[50][50];LL f[50][50];LL dfs(int l,int r){ if(l==r) return a[l]; if(r<l) return 1; LL tot=0; for(int i=l;i<=r;i++) { LL s1=f[l][i-1]?f[l][i-1]:dfs(l,i-1); LL s2=f[i+1][r]?f[i+1][r]:dfs(i+1,r); if(s1*s2+a[i]>tot) { tot=s1*s2+a[i]; way[l][r]=i; } } return f[l][r]=tot;}void Print(int l,int r){ if(l>=r) { if(l==r) printf("%d ",l); return; } printf("%d ",way[l][r]); Print(l,way[l][r]-1); Print(way[l][r]+1,r);}int main(){ //freopen("tree.in","r",stdin); scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%lld",&a[i]); ans=dfs(1,n); printf("%lld\n",ans); Print(1,n); return 0;}
阅读全文
0 0
- NOIP 2003 加分二叉树
- NOIP 2003 加分二叉树
- Noip 2003 加分二叉树
- Noip 2003 加分二叉树
- 加分二叉树 noip
- Noip 2003T3 加分二叉树
- [NOIP 2003] 加分二叉树:DP
- [NOIP提高组2003]加分二叉树
- 加分二叉树 2003年NOIP全国联赛提高组
- 加分二叉树 2003年NOIP全国联赛提高组
- 加分二叉树[NOIP 2003提高组][Codevs 1090]
- 「2003NOIP提高组」加分二叉树
- NOIP 2003 提高组 复赛 加分二叉树
- 加分二叉树
- 1078 加分二叉树
- 加分二叉树_DP
- 加分二叉树
- 二叉树--面试加分
- 三角形类
- project CoberturaMaven: com.ctc.wstx.exc.WstxIOException:Invalid UTF-8 start byte 0xac (at char #1
- Jmeter学习—005—JMeter命令行(非GUI模式)执行详解
- php的PDO
- Linux 软/硬链接
- NOIP 2003 加分二叉树
- java:java.util.Map和java.util.Set的Key类型转换
- Centos之ifconfig 命令找不到(ifconfig command not found) -yellowcong
- 文章标题
- Caused by: org.xml.sax.SAXParseException; 通配符的匹配很全面, 但无法找到元素 'context:property-placeholder' 的声明。
- 学习心得
- 低车速下,扭矩如何增加
- jQuery选择器
- 写给我的JAVA工程师之路-数据表操作