poj-3176 Cow Bowling poj-1163 The Triangle hihocoder #1037 : 数字三角形 (基础dp)
经典的数塔模型。
动态转移方程: dp[i][j]=max(dp[i+1][j],dp[i+1][j+1])+p[i][j];
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <vector> 5 #include <cstring> 6 #include <string> 7 #include <algorithm> 8 #include <string> 9 #include <set> 10 #include <functional> 11 #include <numeric> 12 #include <sstream> 13 #include <stack> 14 #include <map> 15 #include <queue> 16 #pragma comment(linker, "/STACK:102400000,102400000") 17 #define CL(arr, val) memset(arr, val, sizeof(arr)) 18 19 #define ll long long 20 #define inf 0x7f7f7f7f 21 #define lc l,m,rt<<1 22 #define rc m + 1,r,rt<<1|1 23 #define pi acos(-1.0) 24 25 #define L(x) (x) << 1 26 #define R(x) (x) << 1 | 1 27 #define MID(l, r) (l + r) >> 1 28 #define Min(x, y) (x) < (y) ? (x) : (y) 29 #define Max(x, y) (x) < (y) ? (y) : (x) 30 #define E(x) (1 << (x)) 31 #define iabs(x) (x) < 0 ? -(x) : (x) 32 #define OUT(x) printf("%I64d\n", x) 33 #define lowbit(x) (x)&(-x) 34 #define Read() freopen("a.txt", "r", stdin) 35 #define Write() freopen("b.txt", "w", stdout); 36 #define maxn 1000000000 37 #define N 500 38 using namespace std; 39 40 int dp[N][N]; 41 int main() 42 { 43 int n; 44 scanf("%d",&n); 45 for(int i=0;i<n;i++) 46 for(int j=0;j<=i;j++) scanf("%d",&dp[i][j]); 47 for(int i=n-2;i>=0;i--) 48 for(int j=0;j<=i;j++) 49 dp[i][j]=dp[i][j]+max(dp[i+1][j],dp[i+1][j+1]); 50 printf("%d\n",dp[0][0]); 51 return 0; 52 }
只用一维数组。注意 保证下一层的数由上一层推出,不受同一层的干扰。
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <vector> 5 #include <cstring> 6 #include <string> 7 #include <algorithm> 8 #include <string> 9 #include <set> 10 #include <functional> 11 #include <numeric> 12 #include <sstream> 13 #include <stack> 14 #include <map> 15 #include <queue> 16 #pragma comment(linker, "/STACK:102400000,102400000") 17 #define CL(arr, val) memset(arr, val, sizeof(arr)) 18 19 #define ll long long 20 #define inf 0x7f7f7f7f 21 #define lc l,m,rt<<1 22 #define rc m + 1,r,rt<<1|1 23 #define pi acos(-1.0) 24 25 #define L(x) (x) << 1 26 #define R(x) (x) << 1 | 1 27 #define MID(l, r) (l + r) >> 1 28 #define Min(x, y) (x) < (y) ? (x) : (y) 29 #define Max(x, y) (x) < (y) ? (y) : (x) 30 #define E(x) (1 << (x)) 31 #define iabs(x) (x) < 0 ? -(x) : (x) 32 #define OUT(x) printf("%I64d\n", x) 33 #define lowbit(x) (x)&(-x) 34 #define Read() freopen("a.txt", "r", stdin) 35 #define Write() freopen("b.txt", "w", stdout); 36 #define maxn 1000000000 37 #define N 500 38 using namespace std; 39 40 int dp[N]; 41 int main() 42 { 43 //Read(); 44 int n,a,r=0; 45 scanf("%d",&n); 46 for(int i=1;i<=n;i++) 47 { 48 for(int j=i;j>=1;j--) 49 { 50 scanf("%d",&a); 51 dp[j]=max(dp[j],dp[j-1])+a; 52 r=max(r,dp[j]); 53 //printf("%d ",dp[j]); 54 } 55 //printf("\n"); 56 } 57 printf("%d\n",r); 58 return 0; 59 }
转载于:https://www.cnblogs.com/nowandforever/p/4436749.html
poj-3176 Cow Bowling poj-1163 The Triangle hihocoder #1037 : 数字三角形 (基础dp)相关推荐
- Cow Bowling POJ - 3176(基础的动态规划算法)
题意: 杨辉三角,让从顶部开始走到底部,所经过的每一层的点数相加,使得实现最高和. 题目: The cows don't use actual bowling balls when they go b ...
- H - Cow Contest POJ - 3660(Floyd 传递闭包)
H - Cow Contest POJ - 3660 题意: 有 n 头牛比赛,边 1 -> 2 代表 1 能赢 2 ,给你 m 条边,问能确定出多少头牛的名次? 思路: 如果 1->2 ...
- D - Silver Cow Party POJ - 3268
D - Silver Cow Party POJ - 3268 dijkstra 是 O(n2),堆优化一下, O(nlogn) 对每个点跑一次 dj, 取 max(dis(x->i)+dis( ...
- POJ 3660 Cow Contest [Floyd]
POJ - 3660 Cow Contest http://poj.org/problem?id=3660 N (1 ≤ N ≤ 100) cows, conveniently numbered 1. ...
- poj 1985 Cow Marathon 【树的直径】
求树的直径 /* POJ:1985 Cow Marathon 2014/10/12/21:18 Yougth*/ #include <cstdio> #include <iostre ...
- POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)
POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...
- NOI 1163:The Triangle
http://bailian.openjudge.cn/practice/1163/ 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) 图1显示了一个数字三角形.编写一 ...
- 90 数字三角形(Triangle)
文章目录 1 题目 2 解决方案 2.1 思路和图解 2.1.1 遍历法和分治法 2.1.2 带记忆化搜索的分治法 2.1.3 至底向上的动态规划 2.1.4 至顶向下的动态规划 2.3 时间复杂度 ...
- OpenJudge/Poj 1163 The Triangle
1.链接地址: http://bailian.openjudge.cn/practice/1163 http://poj.org/problem?id=1163 2.题目: 总时间限制: 1000ms ...
- POJ 1163 The Triangle
2019独角兽企业重金招聘Python工程师标准>>> DP 入门题 #include <iostream> #include <stdio.h> using ...
最新文章
- ggtree美颜进化树-宏基因组扩增子
- 【POJ 1679 The Unique MST】最小生成树
- 统一配置数据库连接符的方法
- pandoc讲html转换为pdf,mac下利用Pandoc、LaTeX 转换markdown成html,pdf
- Openssl-MD5
- 神经网络基本原理简明教程-0-Python-Numpy库的点滴
- java io 缓存读取_Java 文件IO写入读取四种方法
- 【Elasticsearch】字符过滤器
- Zookeeper启动和Leader选举机制
- OCR文字识别技术总结(二)
- ln -s命令 linux_浅谈linux中ln命令,附带案例
- c++如何关闭进程,比如网吧收银系统
- linux sdr 2832u软件无线电,使用R820T+RTL2832U玩软件无线电
- 想给我们的线下分享会起个名字,求建议!
- eclipse安装和jdk安装(windows平台)
- 微信域名拦截检测API接口
- 工业智能网关BL110应用之62:如何实现智能楼宇控制BACnet 接入亚马逊云平台
- java.util.sortedmap_Java SortedMap lastKey()用法及代码示例
- 技术前沿与经典文章8:智能家居发展白皮书——全屋智能解决方案
- openSUSE桌面环境日常软件推荐
热门文章
- JQUERY弹层|图片滚动|插件开发|上传 相关收藏
- Spring @Aspect、@Before、@After 注解实现 AOP 切面功能
- Linux 命令之 scp 文件传输
- ThreadPoolExecutor 线程池理论、饱和策略、工作队列排队策略
- Centos7忘记mysql的root用户密码
- ffmpeg格式转换命令
- win7上Android环境搭建以及调试
- vs code调试console程序报错--preLaunchTask“build”
- 树形dp贪吃的九头龙(vijos1523)
- [Node.js]001.安装与环境配置