The Triangle POJ - 1163 递推与记忆化搜索
时间:2020-04-21
本文章向大家介绍The Triangle POJ - 1163 递推与记忆化搜索,主要包括The Triangle POJ - 1163 递推与记忆化搜索使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
(Figure 1)
Input
Your program is to read from standard input. The first line contains one integer N: the number of rows in the triangle. The following N lines describe the data of the triangle. The number of rows in the triangle is > 1 but <= 100. The numbers in the triangle, all integers, are between 0 and 99.
Output
Your program is to write to standard output. The highest sum is written as an integer.
Sample Input
5 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5
Sample Output
30
递推代码O(n
2
)
//#include <bits/stdc++.h> #include <iostream> #include <cstdio> #include <cmath> #include<cstring> #include <algorithm> #include <queue> #include<map> using namespace std; typedef long long ll; const ll inf = 1e13; const int mod = 1000000007; const int mx = 150; //check the limits, dummy typedef pair<int, int> pa; const double PI = acos(-1); ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } #define swa(a,b) a^=b^=a^=b #define re(i,a,b) for(int i=(a),_=(b);i<_;i++) #define rb(i,a,b) for(int i=(b),_=(a);i>=_;i--) #define clr(a) memset(a, 0, sizeof(a)) #define lowbit(x) ((x)&(x-1)) #define mkp make_pai //void sc(int& x) { scanf("%d", &x); }void sc(int64_t& x) { scanf("%lld", &x); }void sc(double& x) { scanf("%lf", &x); }void sc(char& x) { scanf(" %c", &x); }void sc(char* x) { scanf("%s", x); } int n, m, k,ans; int high[mx]; int a[mx][mx], dp[mx][mx]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); while (~scanf("%d",&n)) { clr(dp),clr(a); re(i, 1, n + 1)re(j,1,i+1)scanf("%d",&a[i][j]); re(j, 1, n + 1)dp[n][j] = a[n][j]; for (int i = n - 1; i >= 1; i--) for (int j = 1; j <= i; j++) dp[i][j] = a[i][j] + max(dp[i + 1][j], dp[i + 1][j + 1]); printf("%d\n",dp[1][1]); } return 0; }
下面用递推+记忆化搜索也是O(n2)
//#include <bits/stdc++.h> #include <iostream> #include <cstdio> #include <cmath> #include<cstring> #include <algorithm> #include <queue> #include<map> using namespace std; typedef long long ll; const ll inf = 1e13; const int mod = 1000000007; const int mx = 150; //check the limits, dummy typedef pair<int, int> pa; const double PI = acos(-1); ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } #define swa(a,b) a^=b^=a^=b #define re(i,a,b) for(int i=(a),_=(b);i<_;i++) #define rb(i,a,b) for(int i=(b),_=(a);i>=_;i--) #define clr(a) memset(a, -1, sizeof(a)) #define lowbit(x) ((x)&(x-1)) #define mkp make_pai //void sc(int& x) { scanf("%d", &x); }void sc(int64_t& x) { scanf("%lld", &x); }void sc(double& x) { scanf("%lf", &x); }void sc(char& x) { scanf(" %c", &x); }void sc(char* x) { scanf("%s", x); } int n, m, k,ans; int high[mx]; int a[mx][mx], dp[mx][mx]; int dfs(int i, int j) { if (i == n)return a[i][j]; if (dp[i][j] >= 0)return dp[i][j]; return dp[i][j] = max(dfs(i + 1, j), dfs(i + 1, j + 1)) + a[i][j]; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); while (~scanf("%d",&n)) { clr(dp); re(i, 1, n + 1)re(j,1,i+1)scanf("%d",&a[i][j]); printf("%d\n",dfs(1,1)); } return 0; }
原文地址:https://www.cnblogs.com/xxxsans/p/12744037.html
- 两个四字母域名均以五位数被交易
- Flash/Flex学习笔记(15):FMS 3.5之远程共享对象(Remote Shared Object)
- Android Fragment完全解析
- Centos下堡垒机Jumpserver V3.0环境部署完整记录(2)-配置篇
- Flash/Flex学习笔记(53):利用FMS快速创建一个文本聊天室
- 28家银行用户体验调研报告:洞见银行业的“进化论”
- 性能计数器数据收集服务
- SQL SERVER 内存分配及常见内存问题 DMV查询
- 6 利用Docker .NET应用程序模板制作您的容器应用程序(第2部分)
- Mesos+Zookeeper+Marathon的Docker管理平台部署记录(1)
- git review报错一例
- Nginx采用https加密访问后出现的问题
- 对比git rm和rm的使用区别
- Gerrit日常操作命令收集
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- caffe详解之工具篇
- caffe详解之Python接口
- caffe详解之mnist手写体识别
- Python数据分析之基础篇(一)
- Python数据分析之基础篇(二)
- linux操作系统df、du命令
- Python数据分析之基础篇(三)
- Python数据分析之matplotlib(基础篇)
- Python数据分析之matplotlib(提高篇)
- Python数据分析之matplotlib(应用篇)
- Intel OpenCL 之 Pipeline(一):基本概念
- Intel OpenCL 之 Pipeline(二)For循环的执行机制
- Intel OpenCL 之 Pipeline(三)不能pipeline的可能情况
- Intel OpenCL 之 Pipeline(四):Pipeline不理想的几种情况
- Python数据分析之NumPy(基础篇)