codechef Sums in a Triangle题解
Let's consider a triangle of numbers in which a number appears in the first line, two numbers appear in the second line, three in the third line, etc. Develop a program which will compute the largest of the sums of numbers that appear on the paths starting
from the top towards the base, so that:
- on each path the next number is located on the row below, more precisely either directly below or below and one place to the right;
- the number of rows is strictly positive, but less than 100
- all numbers are positive integers between O and 99.
Input
In the first line integer n - the number of test cases (equal to about 1000).
Then n test cases follow. Each test case starts with the number of lines which is followed by their content.
Output
For each test case write the determined value in a separate line.
Example
- Input:
- 2
- 3
- 1
- 2 1
- 1 2 3
- 4
- 1
- 1 2
- 4 1 2
- 2 3 1 1
- Output:
- 5
- 9
使用动态规划法解决本题。
和一题leetcode题一样,从底往上查找路径。
- #include <vector>
- #include <string>
- #include <algorithm>
- #include <stdio.h>
- #include <iostream>
- using namespace std;
- int triPath(vector<vector<int> > &tri)
- {
- if (tri.empty()) return 0;
- vector<int> path(tri.back());
- for (int i = (int)tri.size() - 2; i >= 0 ; i--)//unsigned做减法会溢出!!!
- {
- for (int j = 0; j < (int)tri[i].size(); j++)
- {
- path[j] = max(path[j], path[j+1]) + tri[i][j];
- }
- }
- return path.front();
- }
- int SumsinATriangle()
- {
- int T, n;
- scanf("%d", &T);
- while (T--)
- {
- scanf("%d", &n);
- vector<vector<int> > tri;
- for (int i = 1; i <= n; i++)
- {
- vector<int> tmp(i);
- for (int j = 0; j < i; j++)
- {
- scanf("%d", &tmp[j]);
- }
- tri.push_back(tmp);
- }
- printf("%d\n", triPath(tri));
- }
- return 0;
- }
codechef Sums in a Triangle题解的更多相关文章
- ZOJ 4081 Little Sub and Pascal's Triangle 题解
ZOJ 4081 Little Sub and Pascal's Triangle 题解 题意 求杨辉三角第n行(从1开始计数)有几个奇数. 考察的其实是杨辉--帕斯卡三角的性质,或者说Gould's ...
- Codechef Not a Triangle题解
找出一个数组中的三个数,三个数不能组成三角形. 三个数不能组成三角形的条件是:a + b < c 两边和小于第三边. 这个问题属于三个数的组合问题了.暴力法可解,可是时间效率就是O(n*n*n) ...
- CodeChef November Challenge 2013 部分题解
http://www.codechef.com/NOV13 还在比...我先放一部分题解吧... Uncle Johny 排序一遍 struct node{ int val; int pos; }a[ ...
- SPOJ #453. Sums in a Triangle (tutorial)
It is a small fun problem to solve. Since only a max sum is required (no need to print path), we can ...
- codechef February Challenge 2018 简要题解
比赛链接:https://www.codechef.com/FEB18,题面和提交记录是公开的,这里就不再贴了 Chef And His Characters 模拟题 Chef And The Pat ...
- codechef Row and Column Operations 题解
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- codechef January Lunchtime 2017简要题解
题目地址https://www.codechef.com/LTIME44 Nothing in Common 签到题,随便写个求暴力交集就行了 Sealing up 完全背包算出得到长度≥x的最小花费 ...
- codechef January Challenge 2017 简要题解
https://www.codechef.com/JAN17 Cats and Dogs 签到题 #include<cstdio> int min(int a,int b){return ...
- CF336A Vasily the Bear and Triangle 题解
Content 一个矩形的顶点为 \((0,0)\),其对顶点为 \((x,y)\),现过 \((x,y)\) 作直线,分别交 \(x\) 轴和 \(y\) 轴于 \(A,B\) 两点,使得 \(\t ...
随机推荐
- 【C语言】超大数乘法运算
昨天做排列组合的时候遇到A(a,b)这个问题,需要计算A(20,20)超大,计算机32位的,最大数只能是2^32,这让我很悲伤! 于是乎就自己研究了如何进行超大数的计算! /************* ...
- 重操JS旧业第二弹:数据类型与类型转换
一 数据类型 1 js中的数据类型 1.1 数据类型列举 1)number类型 2)boolean类型 3)string类型 4)对象类型 5)函数类型 6)undefined类型 1.2 数据类型获 ...
- js点击button按钮跳转到另一个新页面
点击按钮怎么跳转到另外一个页面呢?我们在网站制作中可能是需要的,因为有时我们需要做这样的效果,尤其是将按钮做成一个图片,而点击图片要跳转到新的页面时,怎么做到呢? 这样的效果可以:onclick=&q ...
- Android手势识别(单击 双击 抬起 短按 长按 滚动 滑动)
对于触摸屏,其原生的消息无非按下.抬起.移动这几种,我们只需要简单重载onTouch或者设置触摸侦听器setOnTouchListener即可进行处理.不过,为了提高我们的APP的用户体验,有时候我们 ...
- 查找后去掉EditTextView的焦点
//在按钮点击事件里处理 bt_search.setOnClickListener(new OnClickListener() { public void onClick(View v) { ...
- Ext JS4百强应用:设置textfield的悬浮提示信息 --第8强
在Extjs4中有时候我们需要textfield的提示信息,但是我们发现textfield并没有这样的配置项. 这时候我们就要另想方法:我们需要在鼠标悬停在textfield组件的时候进行信息的提示, ...
- HTML5 实现拖拽
如图 可以从第一个方框拖拽花色到第二个方框中. 也可以再拖动回来. 具体代码实现 index.html <!DOCTYPE HTML> <html> <head> ...
- MongoDB学习笔记(三) 在MVC模式下通过Jqgrid表格操作MongoDB数据
看到下图,是通过Jqgrid实现表格数据的基本增删查改的操作.表格数据增删改是一般企业应用系统开发的常见功能,不过不同的是这个表格数据来源是非关系型的数据库MongoDB.nosql虽然概念新颖,但是 ...
- Spinner的用法实现
界面上只有一个textview和一个spinner,实现下拉列表框. spinner.xml: <?xml version="1.0" encoding="utf- ...
- 对数的操作 开始我的JAVA历程
package Text; public class Sumn { public static void main (String args[]){ System.out.println(" ...