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

Hint

Explanation of the sample:

 7 
*

3 8
*

8 1 0
*

2 7 4 4
*

4 5 2 6 5

The highest score is achievable by traversing the cows as shown above.

题意:每次向下或者向右下走,求最大和
分析:正向:每步来源于上方或者左上方,dp[i][j]表示第i行第j列的最大值
dp[i][j]=max{dp[i-1][j],dp[i-1][j-1]}+a[i][j].

#include<stdio.h>
#include<algorithm>
using namespace std;
int n,a[][],ans[][],maxans;
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++)
for(int j=;j<=i;j++)
scanf("%d",&a[i][j]);
for(int i=;i<=n;i++){
for(int j=;j<=i;j++){
ans[i][j]=max(ans[i-][j],ans[i-][j-])+a[i][j];
maxans=max(ans[i][j],maxans);
}
}
printf("%d",maxans);
return ;
}
逆向:逆着从n-1行到第1行,每次比较下方和右下方的大小,大的加上去,最后输出a[1][1]即可。

#include<stdio.h>
#include<algorithm>
using namespace std;
int n,i,j,a[][];
int main(){
scanf("%d",&n);
for(i=;i<=n;i++)
for(j=;j<=i;j++)
scanf("%d",&a[i][j]);
for(i=n-;i>=;i--)
for(j=;j<=i;j++)
a[i][j]+=max(a[i+][j],a[i+][j+]);
printf("%d",a[][]);
return ;
}

【POJ 3176】Cow Bowling(DP)的更多相关文章

  1. 【POJ 3176】Cow Bowling

    题 Description The cows don't use actual bowling balls when they go bowling. They each take a number ...

  2. 【POJ - 3045】Cow Acrobats (贪心)

    Cow Acrobats Descriptions 农夫的N只牛(1<=n<=50,000)决定练习特技表演. 特技表演如下:站在对方的头顶上,形成一个垂直的高度. 每头牛都有重量(1 & ...

  3. 【poj 3167】Cow Patterns(字符串--KMP匹配+数据结构--树状数组)

    题意:给2个数字序列 a 和 b ,问按从小到达排序后,a中的哪些子串与b的名次匹配. a 的长度 N≤100,000,b的长度 M≤25,000,数字的大小 K≤25. 解法:[思考]1.X 暴力. ...

  4. BZOJ 2296【POJ Challenge】随机种子(构造)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2296 [题目大意] 给出一个数x,求一个10的16次以内的数使得其被x整除并且数字包含 ...

  5. 【bzoj 1190】梦幻岛宝珠(DP)

    这题是在01背包问题的基础上,扩充了重量,需要用时间换空间. 思路: 1.仔细看题,注意到重量wi为a*2^b(a<=10,b<=30),很容易想到要按 b 分开做背包的DP.接下来的重点 ...

  6. 【POJ 1273】Drainage Ditches(网络流)

    一直不明白为什么我的耗时几百毫秒,明明差不多的程序啊,我改来改去还是几百毫秒....一个小时后:明白了,原来把最大值0x3f(77)取0x3f3f3f3f就把时间缩短为16ms了.可是为什么原来那样没 ...

  7. 【POJ - 3984】迷宫问题(dfs)

    -->迷宫问题 Descriptions: 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0 ...

  8. 【POJ - 3176】牛保龄球 (简单dp)

    牛保龄球 直接中文了 Descriptions 奶牛打保龄球时不使用实际的保龄球.它们各自取一个数字(在0..99范围内),然后排成一个标准的保龄球状三角形,如下所示: 7 3 8 8 1 0 2 7 ...

  9. 【POJ 3167】Cow Patterns (KMP+树状数组)

    Cow Patterns Description A particular subgroup of K (1 <= K <= 25,000) of Farmer John's cows l ...

随机推荐

  1. CF613D Kingdom and its Cities 虚树

    传送门 $\sum k \leq 100000$虚树套路题 设$f_{i,0/1}$表示处理完$i$以及其所在子树的问题,且处理完后$i$所在子树内是否存在$1$个关键点满足它到$i$的路径上不存在任 ...

  2. 51Nod 1443 路径和树

    还是一道很简单的基础题,就是一个最短路径树的类型题目 我们首先可以发现这棵树必定满足从1出发到其它点的距离都是原图中的最短路 换句话说,这棵树上的每一条边都是原图从1出发到其它点的最短路上的边 那么直 ...

  3. shell脚本中的数据传递方式

    shell中支持的数据传递方式 主要有那么几种: 变量.管道.结果引用.重定向+文件.以及xargs. 变量方式: 1. 定义变量: 变量名=值 2. 使用变量: $变量名 管道方式: 统计当前文件夹 ...

  4. GATT服务搜索流程(二)

    关于bta_dm_cb.p_sec_cback,这里我们之前已经分析过,他就是bte_dm_evt ,最终调用的函数btif_dm_upstreams_evt : static void btif_d ...

  5. .NET持续集成与自动化部署之路第二篇——使用NuGet.Server搭建公司内部的Nuget(包)管理器

    使用NuGet.Server搭建公司内部的Nuget(包)管理器 前言     Nuget是一个.NET平台下的开源的项目,它是Visual Studio的扩展.在使用Visual Studio开发基 ...

  6. layer.conifrm 非阻塞执行 ztree删除节点 问题

    layer.confirm无法阻塞js执行,导致ztree插件的beforeRemove回调函数未等待用户确定删除便已经移除界面中的节点, 因此可能会出现前后台数据不一致情况,正常逻辑理应删除后台数据 ...

  7. 我的devops实践经验分享一二

    前言 随着系统越来越大,开发人员.站点.服务器越来越多,微服务化推进,......等等原因,实现自动化的devops越来越有必要. 当然,真实的原因是,在团队组建之初就预见到了这些问题,所以从一开始就 ...

  8. C/C++中int128的那点事

    最近群友对int128这个东西讨论的热火朝天的.讲道理的话,编译器的gcc是不支持__int128这种数据类型的,比如在codeblocks 16.01/Dev C++是无法编译的,但是提交到大部分O ...

  9. 变量 var &函数new

    声明变量 变量:变量是存储信息的容器,创建变量通常称为"声明"变量 变量必须以字母开头(小驼峰式myName): 变量也能以 $ 和 _ 符号开头(不过我们不推荐这么做): 变量名 ...

  10. 【CV】ICCV2015_Unsupervised Learning of Spatiotemporally Coherent Metrics

    Unsupervised Learning of Spatiotemporally Coherent Metrics Note here: it's a learning note on the to ...