4979 数塔

 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 const int N=61;
 5 int f[N][N];
 6 int ff[N][N];
 7 int ans[N/2];
 8 int main()
 9 {
10     int n;
11     cin>>n;
12     for(int i=1;i<=n;i++)
13      {
14          for(int j=1;j<=i;j++)
15           {
16               cin>>f[i][j];
17               ff[i][j]=f[i][j];
18           }
19      }
20      for(int i=n-1;i>=1;i--)
21       {
22           for(int j=1;j<=i;j++)
23            {
24                f[i][j]+=max(f[i+1][j],f[i+1][j+1]);
25            }
26       }
27      ans[1]=ff[1][1];
28      int k=f[2][1]>f[2][2]?1:2;
29      int i=2;
30      while(i<=n)
31       {
32           ans[i]=ff[i][k];
33           k=f[i+1][k]>f[i+1][k+1]?k:k+1;
34           i++;
35       }
36       cout<<f[1][1]<<endl;
37       for(int i=1;i<=n-1;i++) 
38        {
39            cout<<ans[i]<<"-";
40        }
41        cout<<ans[n];
42        return 0;
43 }

4979 数塔

 

 时间限制: 1 s
 空间限制: 1000 KB
 题目等级 : 黄金 Gold
 
 
题目描述 Description

有如图数塔,从顶部出发,在每一节点选择向左走或向右走,一直走到底。要求找到一个路径,使路径总和最大。

                               13

                          11       8

                     12       7      26

                  6      14      15      8

            12      7      13      24     11

输入描述 Input Description

第一行输入一正整数n

接下来从第2行到第n+1行每行输入行数减1个正整数m。

输出描述 Output Description

共两行

第一行输出最大总和;

第二行输出最大路径,用‘-’隔开;

样例输入 Sample Input

5

13

11   8

12   7  26

 6  14  15   8

12   7  13  24  11

样例输出 Sample Output

86

13-8-26-15-24

数据范围及提示 Data Size & Hint

40%数据:n<=20 , m<=1000

60%数据:n<=60 , m<=1050

原文地址:https://www.cnblogs.com/lyqlyq/p/6763677.html