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 ...
随机推荐
- winform窗体全屏
bool fullscreen = false;Rectangle rect = new Rectangle();private void button4_Click(object sender, E ...
- Microsoft office PPT 2007 保存时速度慢(整理自网上)
问题描述: XP sp3上运行PPT2007,当需要保存文件时,发现竟然需要近1分钟才能保存完毕,其间可能会出现“瘟都死沙漏”来提示你正在保存. 这简直慢到过分慢到无法容忍,一开始以为是ppt文件过大 ...
- K. Perpetuum Mobile
The world famous scientist Innokentiy almost finished the creation of perpetuum mobile. Its main par ...
- delphi删除只读文件
只读文件就是不能删除的文件,用DeleteFile函数对它来说是毫无意义的,要删除只读文件,只有先改变它的属性.如果你要删除一个文件,最好先作两个方面的考虑: (1)判断该文件的属性.可以用上面提到的 ...
- asp.net iis URLRewrite 实现方法详解
原文 asp.net iis URLRewrite 实现方法详解 实现非常简单首先你要在你的项目里引用两个dll:actionlessform.dll.urlrewriter.dll,真正实现重写的是 ...
- 四种方法解决DIV高度自适应问题
本文和大家重点讨论一下解决DIV高度自适应的方法,这里主要从四个方面来向大家介绍,相信通过本文学习你对DIV高度自适应问题会有更加深刻的认识. DIV高度自适应 关于DIV高度的自适应,一直是个让人头 ...
- Xamarin相关学习预估
以前没有开发过app也没有了解过,当然只是用过,现在迫于形势无奈活到老学到老. 初步想了下app相关开发所涉及的知识点 一 app相关资源监视 1.1 网络监视器:https://github.co ...
- 外国的Delphi网站
www.phidels.com delphifr.com http://www.swissdelphicenter.com/torry/showcode.php?id=787 B4A delphifa ...
- must return an Iterable of arrays.(junit4)
java.lang.Exception: TestIterator.init() must return an Iterable of arrays. at org.junit.runners.Par ...
- 14.2.5.6 Adaptive Hash Indexes 自适应Hash Indexes
14.2.5.6 Adaptive Hash Indexes 自适应Hash Indexes adaptive hash index(AHI) 让InnoDB 执行更加像在一个内存数据库里在, 在不牺 ...