递推DP POJ 1163 The Triangle
题意:找一条从顶部到底部的一条路径,往左下或右下走,使得经过的数字和最大。
分析:递推的经典题目,自底向上递推。当状态保存在a[n][j]时可省去dp数组,空间可优化。
代码1:
/************************************************
* Author :Running_Time
* Created Time :2015-9-1 11:17:04
* File Name :POJ_1163.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 1e2 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int a[N][N];
int dp[N][N]; int main(void) {
int n;
while (scanf ("%d", &n) == 1) {
for (int i=1; i<=n; ++i) {
for (int j=1; j<=i; ++j) {
scanf ("%d", &a[i][j]);
if (i == n) dp[i][j] = a[i][j];
}
}
for (int i=n-1; i>=1; --i) {
for (int j=1; j<=i; ++j) {
dp[i][j] = max (dp[i+1][j], dp[i+1][j+1]) + a[i][j];
}
}
printf ("%d\n", dp[1][1]);
} return 0;
}
代码2(空间优化):
/************************************************
* Author :Running_Time
* Created Time :2015-9-1 11:18:25
* File Name :POJ_1163_2.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 1e2 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int a[N][N]; int main(void) {
int n;
while (scanf ("%d", &n) == 1) {
for (int i=1; i<=n; ++i) {
for (int j=1; j<=i; ++j) {
scanf ("%d", &a[i][j]);
}
}
for (int i=n-1; i>=1; --i) {
for (int j=1; j<=i; ++j) {
a[n][j] = max (a[n][j], a[n][j+1]) + a[i][j];
}
}
printf ("%d\n", a[n][1]);
} return 0;
}
递推DP POJ 1163 The Triangle的更多相关文章
- poj 2229 【完全背包dp】【递推dp】
poj 2229 Sumsets Time Limit: 2000MS Memory Limit: 200000K Total Submissions: 21281 Accepted: 828 ...
- 递推DP URAL 1167 Bicolored Horses
题目传送门 题意:k个马棚,n条马,黑马1, 白马0,每个马棚unhappy指数:黑马数*白马数,问最小的unhappy值是多少分析:dp[i][j] 表示第i个马棚放j只马的最小unhappy值,状 ...
- 递推DP URAL 1017 Staircases
题目传送门 /* 题意:给n块砖头,问能组成多少个楼梯,楼梯至少两层,且每层至少一块砖头,层与层之间数目不能相等! 递推DP:dp[i][j] 表示总共i块砖头,最后一列的砖头数是j块的方案数 状态转 ...
- 递推DP URAL 1260 Nudnik Photographer
题目传送门 /* 递推DP: dp[i] 表示放i的方案数,最后累加前n-2的数字的方案数 */ #include <cstdio> #include <algorithm> ...
- 递推DP URAL 1353 Milliard Vasya's Function
题目传送门 /* 题意:1~1e9的数字里,各个位数数字相加和为s的个数 递推DP:dp[i][j] 表示i位数字,当前数字和为j的个数 状态转移方程:dp[i][j] += dp[i-1][j-k] ...
- 递推DP URAL 1119 Metro
题目传送门 /* 题意:已知起点(1,1),终点(n,m):从一个点水平或垂直走到相邻的点距离+1,还有k个抄近道的对角线+sqrt (2.0): 递推DP:仿照JayYe,处理的很巧妙,学习:) 好 ...
- 递推DP 赛码 1005 Game
题目传送门 /* 递推DP:官方题解 令Fi,j代表剩下i个人时,若BrotherK的位置是1,那么位置为j的人是否可能获胜 转移的时候可以枚举当前轮指定的数是什么,那么就可以计算出当前位置j的人在剩 ...
- 递推DP HDOJ 5328 Problem Killer
题目传送门 /* 递推DP: 如果a, b, c是等差数列,且b, c, d是等差数列,那么a, b, c, d是等差数列,等比数列同理 判断ai-2, ai-1, ai是否是等差(比)数列,能在O( ...
- hdu1978(递推dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1978 分析: 递推DP. dp[][]表示可以到达改点的方法数. 刚开始:外循环扫描所有点dp[x][ ...
随机推荐
- Word Amalgamation(枚举 + 排序)
Word Amalgamation Time Limit: 1 Sec Memory Limit: 64 MB Submit: 373 Solved: 247 Description In mil ...
- OpenGL顶点缓冲区对象(VBO)
转载 http://blog.csdn.net/dreamcs/article/details/7702701 创建VBO GL_ARB_vertex_buffer_object 扩展可 ...
- SIFT+HOG+鲁棒统计+RANSAC
今天的计算机视觉课老师讲了不少内容,不过都是大概讲了下,我先记录下,细讲等以后再补充. SIFT特征: 尺度不变性:用不同参数的高斯函数作用于图像(相当于对图像进行模糊,得到不同尺度的图像),用得到的 ...
- LBP
参考:http://www.cnblogs.com/mikewolf2002/p/3438698.html
- myeclipse2014破解过程
之前装的是10,后来没事试试装了2014,然后再破解2014后发现2010的证书就失效了,之前在网上也没找到方法,这段时间也没管,今天又自己想办法试了试,发现成功了!下边是我在网上找的破解方法的破解步 ...
- ubuntu 12.04 server编译安装nginx
tar -xvf zlib-1.2.8.tar.gz cd zlib-1.2.8 ./config make make install above is for zlib(refers http:// ...
- cookie注入的形成,原理,利用总结
一:cookie注入的形成 程序对提交数据获取方式是直接request("c.s.t")的方式.未指明使用request对象的具体方法进行获取. 二:原理 request(&quo ...
- 【云计算】Dockerfile、镜像、容器快速入门
Dockerfile.镜像.容器快速入门 1.1.Dockerfile书写示例 Dockerfile可以用来生成Docker镜像,它明确的定义了Image的生成过程.虽然直接修改容器也可以提交生成镜像 ...
- CHM文档打开空白的解决
网上打包的CHM格式的文档,有时候打开无论点击目录哪一章节都会出现一片空白或者显示已取消到该网页的导航 这个情况的原因就是CHM文件在Windows的HTFS文件系统中会默认被阻止显示,解决方法就是在 ...
- 【JAVA、C++】LeetCode 014 Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. 解题思路: 老实遍历即可, ...