数塔

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 30157    Accepted Submission(s): 18041

Problem Description
在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的:

有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少?

已经告诉你了,这是个DP的题目,你能AC吗?

 
Input
输入数据首先包括一个整数C,表示测试实例的个数,每个测试实例的第一行是一个整数N(1 <= N <= 100),表示数塔的高度,接下来用N行数字表示数塔,其中第i行有个i个整数,且所有的整数均在区间[0,99]内。
 
Output
对于每个测试实例,输出可能得到的最大和,每个实例的输出占一行。
 
Sample Input
1
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
 
Sample Output
30
从下往上遍历,每个节点取去最大权和:dp[k][i]=max(dp[k+1][i]+a[i+t],dp[k+1][i+1]+a[i+t]);
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define Max 6000
using namespace std;
int a[Max];
int dp[][];
/*int maxx(int a,int b)
{
return a>b?a:b;
}*/
int main()
{
int T,n,p;
int i,j,k;
freopen("in.txt","r",stdin);
scanf("%d",&T);
while(T--)
{
memset(dp,,sizeof(dp));
scanf("%d",&n);
p=(n+)*n/;
for(i=;i<=p;i++)
scanf("%d",&a[i]);
for(k=n;k>=;k--)
{
int t=(k-+)*(k-)/;
for(i=;i<=k;i++)
dp[k][i]=max(dp[k+][i]+a[i+t],dp[k+][i+]+a[i+t]);
}
printf("%d\n",dp[][]);
}
}

数塔(dp)的更多相关文章

  1. HDU 2084 数塔 (DP)

    数塔 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Pr ...

  2. HDU2084 数塔 (DP入门题)

    数塔 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...

  3. HDU2084:数塔(DP)

    Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大 ...

  4. 数塔~~dp学习_1

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2084 数塔 Time Limit: 1000/1000 MS (Java/Others)    Mem ...

  5. USACO training course Number Triangles 数塔 /// DP oj10122

    题目大意: ...就是数塔       7         3   8      8   1   0     2   7   4   4 4   5   2   6   5 7+3+8+7+5=30 ...

  6. [hdu1176]免费馅饼(数塔dp)

    题意:中文题,不解释了 = = 解题关键:逆推,转化为数塔dp就可以了 dp[i][j]表示在i秒j位置的最大值. 转移方程:$dp[i][j] = \max (dp[i + 1][j],dp[i + ...

  7. HDU 1176 免费馅饼(数塔dp)

    一开始被吓到了,后来再仔细一读发现就是一个数塔,没有那么复杂 #include<stdio.h> #include<string.h> #include<algorith ...

  8. hdu2084 数塔 DP

    数字三角形,DP裸题 #include<stdio.h> #include<string.h> #define max(a,b) (a)>(b)?a:b ][],dp[] ...

  9. dp入门--poj 1163数塔

                                                                                                        ...

  10. HDU-2084 数塔 经典dp,水

    1.HDU-2084   数塔 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=2084 3.总结:从下往上推,最后归于顶点.方程为  dp[i][j] ...

随机推荐

  1. UVa 232 Crossword Answers

     Crossword Answers  A crossword puzzle consists of a rectangular grid of black and white squares and ...

  2. Xshell4连接Linux后 win快捷键锁屏

    今天在使用Xshell连接CentOS后 使用Vim编辑器编辑完后 习惯性的按了Ctrl+S 然后按什么都不起作用 只能重新连接 通过查资料得知 Ctrl + S 是Linux 锁屏的快捷键 要解除锁 ...

  3. Qt如何去掉按钮等控件的虚线框(焦点框)(两种方法)

    方法1:可以通过代码ui->pushButton->setFocusPolicy(Qt::NoFocus)或在Qt Creator的属性列表中设置. 方法2:如果在嵌入式设备中需要通过按键 ...

  4. Qt编程之实现在QFileDialog上添加自定义的widget

    上网搜索找到的方法如下: http://www.qtforum.org/article/20841/how-to-add-a-qwidget-in-qfiledialog.html#post78422 ...

  5. unix c 11

    多线程(thread)    操作系统支持多进程,进程内部使用多线程.    进程是 重量级的,拥有自己 独立的内存空间.    线程是 轻量级的,不需要拥有自己 独立的内存空间,线程的内存空间:1 ...

  6. Find the Celebrity 解答

    Question Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there ma ...

  7. Uva11183-Teen Girl Squad(有向图最小生成树朱刘算法)

    解析: 裸的有向图最小生成树 代码 #include<cstdio> #include<cstring> #include<string> #include< ...

  8. JUnit单元测试框架的使用

    http://blog.csdn.net/mao520741111/article/details/51462215 原文地址 http://www.open-open.com/lib/view/op ...

  9. UGUI Button和Toogle动态添加事件

    如果你想动态创建Button和Toogle 等等一系列控件,需要动态添加事件的如下. 拿button和Toogle抛砖引玉O(∩_∩)O~ using UnityEngine; using Syste ...

  10. 解决getJdbcTemplate往oracle数据库中插入数据返回主键出错问题

    我们使用Spring中的JdbcDaoSupport往Mysql中插入数据并返回主键代码,我们使用的mysql数据库,主键在数据库中设置为自增长:该类继承自JdbcDaoSupport,所以能直接使用 ...