题目描述

Consider the number triangle shown below. Write a program that calculates the highest sum of numbers that can be passed on a route that starts at the top and ends somewhere on the base. Each step can go either diagonally down to the left or diagonally down to the right.
          7
        3   8
      8   1   0
    2   7   4   4
  4   5   2   6   5
In the sample above, the route from 7 to 3 to 8 to 7 to 5 produces the highest sum: 30.

 

输入

The first line contains R (1 <= R <= 1000), the number of rows. Each subsequent line contains the integers for that particular row of the triangle. All the supplied integers are non-negative and no larger than 100.

输出

A single line containing the largest sum using the traversal specified.

样例输入

5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5

样例输出

30

分析:

我就说是个DP题,递什么归递归。

设dp[i][j]表示从底层走到达坐标[i][j]的最大值,从底向上扫,状态转移方程为dp[i][j]+=max(dp[i+1][j],dp[i+1][j+1])。

#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#define range(i,a,b) for(int i=a;i<=b;++i)
#define rerange(i,a,b) for(int i=a;i>=b;--i)
#define fill(arr,tmp) memset(arr,tmp,sizeof(arr))
using namespace std;
int n,dp[][];
void init(){
cin>>n;
range(i,,n)range(j,,i)cin>>dp[i][j];
rerange(i,n-,)
range(j,,i)dp[i][j]+=max(dp[i+][j],dp[i+][j+]);
}
void solve(){
cout<<dp[][]<<endl;
}
int main() {
init();
solve();
return ;
}

Number Triangles的更多相关文章

  1. 【洛谷 P1216】【IOI1994】【USACO1.5】数字三角形 Number Triangles

    (如此多的标签qaq) 数字三角形 Number Triangles[传送门] 本来打算当DP练的,没想到写着写着成递推了(汗) 好的没有时间了,我们附个ac代码(改天不写): #include< ...

  2. P1216 [IOI1994][USACO1.5]数字三角形 Number Triangles

    P1216 [IOI1994][USACO1.5]数字三角形 Number Triangles 这个题吧,之前学DP的时候就做过一次了,其实还是挺简单的,如果一步一步按照找状态定义,找边界条件,找转移 ...

  3. 洛谷——P1216 [USACO1.5]数字三角形 Number Triangles

    P1216 [USACO1.5]数字三角形 Number Triangles 题目描述 观察下面的数字金字塔. 写一个程序来查找从最高点到底部任意处结束的路径,使路径经过数字的和最大.每一步可以走到左 ...

  4. USACO 1.5 Number Triangles

    Number Triangles Consider the number triangle shown below. Write a program that calculates the highe ...

  5. USACO Section 1.5 Number Triangles 解题报告

    题目 题目描述 现在有一个数字三角形,第一行有一个数字,第二行有两个数字,以此类推...,现在从第一行开始累加,每次在一个节点累加完之后,下一个节点必须是它的左下方的那个节点或者是右下方那个节点,一直 ...

  6. 洛谷 1.5.1 Number Triangles 数字金字塔

    Description 考虑在下面被显示的数字金字塔. 写一个程序来计算从最高点开始在底部任意处结束的路径经过数字的和的最大. 每一步可以走到左下方的点也可以到达右下方的点. 7 3 8 8 1 0 ...

  7. [USACO1.5]数字三角形 Number Triangles

    题目描述 观察下面的数字金字塔. 写一个程序来查找从最高点到底部任意处结束的路径,使路径经过数字的和最大.每一步可以走到左下方的点也可以到达右下方的点. 7 3 8 8 1 0 2 7 4 4 4 5 ...

  8. USACO Section1.5 Number Triangles 解题报告

    numtri解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...

  9. P1216 [USACO1.5]数字三角形 Number Triangles

    题目描述 观察下面的数字金字塔. 写一个程序来查找从最高点到底部任意处结束的路径,使路径经过数字的和最大.每一步可以走到左下方的点也可以到达右下方的点. 7 3 8 8 1 0 2 7 4 4 4 5 ...

随机推荐

  1. Python 爬取网页中JavaScript动态添加的内容(二)

    使用 selenium + phantomjs 实现 1.准备环境 selenium(一个用于web应用程测试的工具)安装:pip install seleniumphantomjs(是一种无界面的浏 ...

  2. ssm项目中ueditor富文本编辑器的使用

    一.下载 https://ueditor.baidu.com/website/index.html 将ueditor放到项目中合适的位置 二 . 配置文件上传路径 在utf8-jsp/jsp/conf ...

  3. 洛谷P2678跳石头(提高)

    题目背景 一年一度的“跳石头”比赛又要开始了! 题目描述 这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石.组委会已经选择好了两块岩石作为比赛起点和终点. 在起点和终点之间,有 N 块岩石( ...

  4. Python namedtuple(命名元组)使用实例

    Python namedtuple(命名元组)使用实例 #!/usr/bin/python3 import collections MyTupleClass = collections.namedtu ...

  5. PHP如何实现第三方分享

    <!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. Spring Web MVC 笔记

    Spring Web MVC 流程 Dispatcher Servlet 这是一个前端分派 Servlet(前端控制器模式),外部所有的请求都会先到达这里,然后由其将请求分派给其他组件进行实际的处理. ...

  7. android ListView与EditText共存错位

    在一个ListView中,如果里面有EditText会很麻烦,因为修改EditText里面的数据会发生错位现象. 这时候,需要在适配器BaseAdapter的getView中设置setTag(),将p ...

  8. CSS 3中细线边框如何实现?

    在app应用开发中,我们常常都需要用到css3来设置应用的样式.由于app都是在移动设备上进行展示,所以边框描边的线一般都小于1px,而以往我们使用的都是1px及以上的.那么问题来了,对于小于1px的 ...

  9. altera ip 核小究

    用quartus的MegaWizard工具生成一个乘法器multiplier,会在工程目录下产生 multiplier.qip    (可选) multiplier_bb.v  (可选) multip ...

  10. MySQL的菜鸟级操作

    windows下启动和关闭mysql 启动(windows): 在命令行下,进入到mysql安装路径的bin文件夹下执行:net start mysql 关闭(windows): 在启动的情况下执行: ...