Problem Description
The cows don't use actual bowling balls when they go bowling. They each take a number (in the range 0..99), though, and line up in a standard bowling-pin-like triangle like this:

          7

        3   8

      8   1   0

    2   7   4   4

  4   5   2   6   5

Then the other cows traverse the triangle starting from its tip and moving "down" to one of the two diagonally adjacent cows until the "bottom" row is reached. The cow's score is the sum of the numbers of the cows visited along the way. The cow with the highest score wins that frame.

Given a triangle with N (1 <= N <= 350) rows, determine the highest possible sum achievable.

 
Input
Line 1: A single integer, N

Lines 2..N+1: Line i+1 contains i space-separated integers that represent row i of the triangle.

 
Output
Line 1: The largest sum achievable using the traversal rules
 
Sample Input
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
 
Sample Output
30
 
Source
PKU
题意:给定一个数塔,第i行有i个数。现在需要找到一条从树顶结点遍历到树底结点的最长路径,即遍历的结点中数字相加的和最大。且每个结点只许向其下层相邻的左右两个结点遍历。
 思路:看到题目一开始想用搜索做但是数据范围太大;如果用广搜的话,只是最后一步就需要2*10^105个空间肯定会超时,效率也不高;所以,改用dp;记录到第i行第j列是和的最大值;
 
 #include<iostream>
#include<cstdio> using namespace std; int max(int &a,int &b)
{
return a>b?a:b;
} int main()
{
int n;
cin>>n;
int i,j;
int dp[]={};//dp数组,
int s[]={};//存储数组;
for(i=;i<=n;i++)
{
for(j=;j<=i;j++)//录入第i行
cin>>s[j];
for(j=i;j>;j--)//对dp数组进行跟新;
dp[j]=max(dp[j],dp[j-])+s[j];
}
j=;
for(i=;i<=n;i++)
if(dp[i]>j)j=dp[i];//查找最大值;
cout<<j<<endl;//输出最大值;
return ;
}

杭电三部曲一、基本算法;19题 Cow Bowling的更多相关文章

  1. 2022“杭电杯”中国大学生算法设计超级联赛(6)- 1011 Find different

    2022"杭电杯"中国大学生算法设计超级联赛(6)- 1011 Find different 比赛时队友开摆,还剩半个小时,怎么办?? 当然是一起摆 Solution 看到这个题没 ...

  2. 一个人的旅行 HDU杭电2066【dijkstra算法 || SPFA】

    pid=2066">http://acm.hdu.edu.cn/showproblem.php? pid=2066 Problem Description 尽管草儿是个路痴(就是在杭电 ...

  3. HDU 4901(杭电多校训练#3 1005题)The Romantic Hero(DP)

    题目地址:HDU 4901 这题没想到最后竟然可以做出来.. .. 这题用了两次DP,先从前往后求一次异或的.再从后往前求一次与运算的. 各自是 1:求异或的时候,定义二维数组huo[1000][10 ...

  4. 杭电 1155 Bungee Jumping(物理题)

    Problem Description Once again, James Bond is fleeing from some evil people who want to see him dead ...

  5. HDU 4920(杭电多校训练#5 1010 题) Matrix multiplication(不知道该挂个什么帽子。。。)

    题目地址:pid=4920">HDU 4920 对这个题简直无语到极点. . .竟然O(n^3)的复杂度能过....方法有三.. 1:进行输入优化和输出优化. . (前提是你的输入优化 ...

  6. 畅通project续HDU杭电1874【dijkstra算法 || SPFA】

    http://acm.hdu.edu.cn/showproblem.php?pid=1874 Problem Description 某省自从实行了非常多年的畅通project计划后.最终修建了非常多 ...

  7. 畅通project再续 HDU杭电1875 【Kruscal算法 || Prim】

    Problem Description 相信大家都听说一个"百岛湖"的地方吧.百岛湖的居民生活在不同的小岛中.当他们想去其它的小岛时都要通过划小船来实现.如今政府决定大力发展百岛湖 ...

  8. Choose the best route HDU杭电2680【dijkstra算法 || SPFA】

    http://acm.hdu.edu.cn/showproblem.php?pid=2680 Problem Description One day , Kiki wants to visit one ...

  9. Rikka with Travels(2019年杭电多校第九场07题+HDU6686+树形dp)

    目录 题目链接 题意 思路 代码 题目链接 传送门 题意 定义\(L(a,b)\)为结点\(a\)到结点\(b\)的路径上的结点数,问有种\(pair(L(a,b),L(c,d))\)取值,其中结点\ ...

随机推荐

  1. ASP.NET里面,如果设置了form的 onsubmit="return false;"之后,就不能提交按钮了?

    我的按钮是写成的服务器控件的形式<asp:Button ID="btnSubmitBR" runat="server" Text="提交&quo ...

  2. 洛谷-谁拿了最多奖学金-NOIP2005提高组复赛

    题目描述 Description 某校的惯例是在每学期的期末考试之后发放奖学金.发放的奖学金共有五种,获取的条件各自不同: 1)     院士奖学金,每人8000元,期末平均成绩高于80分(>8 ...

  3. ECOS-Mongodb安装

    安装Mongodb服务 安装Mongodb服务 author :James,jimingsong@vip.qq.com since :2015-03-03 下载Mongodb安装包(64位哦) 安装M ...

  4. [Centos] mod_wsgi 安装流程以及遇到问题解决办法。apxs: command not found 或 Sorry, Python developer package does not appear to be installed.

    前提: Centos 系统, apache 已安装, python 已安装. 1. 首先下载mod_wsgi-3.5.tar.gz 下载地址:https://code.google.com/p/mod ...

  5. 10、 iOS 加载图片选择imageNamed 方法还是 imageWithContentsOfFile

    Apple官方的文档为生成一个UIImage对象提供了两种方法: 1. imageNamed,其参数为图片的名字: 2. imageWithContentsOfFile,其参数是图片文件的路径. 两种 ...

  6. 父子页面(iframe)相互获取对方dom元素

    现在iframe的使用虽然开始越来越少,但是还是有牵涉到iframe的使用情况,特别是多个iframe互相嵌套,又要进行获取的情况. 现在整理了父子iframe之间互相获取的方式. (1)父页面获取子 ...

  7. Drupal设置首页默认内容

    接触Drupal时间不长,记录一下学习点滴~ Drupal首页的内容,默认是取node表的内容展示的,如果想让首页展示自己创表的内容怎么办呢?以Drupal7为例 在这个admin/config/sy ...

  8. Using YARN with Cgroups testing in sparkml cluster

    部署服务器: sparkml 集群 ########### sparkml ########## sparkml-node1 # yarn resource manager sparkml-node2 ...

  9. SpringMVC通过注解获得参数

    SpringMVC可以通过RequestParam注解来映射获得参数,具体用法如下: 例子: 配置过程省略 1.新建controller类 package com.loger.controller; ...

  10. javascript windows对象

    1.windows对象方法 2.计时器方法 3.计时器setInterval() <!DOCTYPE HTML> <html> <head> <meta ht ...