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 ...
随机推荐
- java.exe路径问题
因为要更换JDK版本,自然也就要重新设置JAVA_HOME环境变量,但设置完成后奇怪的发现,运行java -version时还是原来的版本,莫名其妙,最后我把JAVA_HOME环境变量删除竟然java ...
- 24篇HTTP博客
http://www.cppblog.com/woaidongmao/category/11721.html
- Mac下MAMP初试体验
原创文章,转载请注明出处! 近期小学习了一下Mac下的Apache,Mysql,php.这里记录一下,以备忘 1 php 1.1 php返回值的測试 在MAMP下測试成功,直接echo返回所数据 1. ...
- hdu 4081 Qin Shi Huang's National Road System (次小生成树的变形)
题目:Qin Shi Huang's National Road System Qin Shi Huang's National Road System Time Limit: 2000/1000 M ...
- smartforms换页,
smartforms换页,可以把最后的窗口设置成最终窗口, 这样页码有可以用系统变量控制: 总页码/页码 :&SFSY-FORMPAGES&
- 关于在打包Jar文件时遇到的资源路径问题(二)
在关于<关于在打包Jar文件时遇到的资源路径问题(一)>中,以及描述了当资源与可执行JAr分离时的资源路径代码的编写问题,后来想了想,为什么将<Java核心技术卷一>中的程序1 ...
- Spring MVC Controller与jquery ajax请求处理json
在用 spring mvc 写应用的时候发现jquery传递的[json数组对象]参数后台接收不到,多订单的处理,ajax请求: "}]}]} $.ajax({ url : url, typ ...
- HTML5 实现拖拽
如图 可以从第一个方框拖拽花色到第二个方框中. 也可以再拖动回来. 具体代码实现 index.html <!DOCTYPE HTML> <html> <head> ...
- Delphi中使用GDI+进行绘图(2)
2)使用IGDIPlus接口 (1)下载安装所需软件 可以在以下地址下载IGDI+最新的安装程序. http://www.mitov.com/products/igdi+ www.igdiplus.o ...
- JavaScript(一基本语法)
本篇博客是对js的一个基本的了解,对于没有js基础的同学来说应该是个入门的基本吧 javascript 是原型化继承来的面向对象的动态类型的区分大小写的客户端的脚本语言.主要目的是为了解决服务器语言, ...