比赛 |
动规 |
评测结果 |
AAAAAAA |
题目名称 |
加分二叉树 |
最终得分 |
100 |
用户昵称 |
Menamovic |
运行时间 |
0.002 s |
代码语言 |
C++ |
内存使用 |
0.33 MiB |
提交时间 |
2017-06-18 20:51:28 |
显示代码纯文本
- #include<bits/stdc++.h>
-
- using namespace std;
-
- const int e=50;
- const int maxn=-999999999;
- int n;
- int f[e][e];
- int a[e];
- int root[e][e]={0};
-
- void front(int x,int y)
- {
- if(root[x][y]!=0) printf("%d ",root[x][y]);
- if(root[x][root[x][y]-1]!=0) front(x,root[x][y]-1);
- if(root[root[x][y]+1][y]!=0) front(root[x][y]+1,y);
- }
-
- int main()
- {
- freopen("jfecs.in","r",stdin);
- freopen("jfecs.out","w",stdout);
- scanf("%d",&n);
- for(int i=0;i<=n;i++)
- {
- for(int j=0;j<=n;j++)
- {
- f[i][j]=1;
- }
- }
- for(int i=1;i<=n;i++)
- {
- scanf("%d",&a[i]);
- f[i][i]=a[i];
- root[i][i]=i;
- }
- for(int len=1;len<=n;len++)
- {
- for(int i=1;i<=n;i++)
- {
- int j=i+len;
- if(j<=n)
- {
- int temp=maxn;
- for(int k=i;k<=j;k++)
- {
- if(temp<f[i][k-1]*f[k+1][j]+a[k])
- {
- temp=f[i][k-1]*f[k+1][j]+a[k];
- root[i][j]=k;
- }
- }
- f[i][j]=temp;
- }
- }
- }
- printf("%d\n",f[1][n]);
- front(1,n);
- return 0;
- }