Book 动态规划
虽然之前学过一点点,但是还是不会------现在好好跟着白书1.4节学一下——————
(1)数字三角形
d(i,j) = max(d(i+1,j),d(i+1,j+1)) + a[i][j]
hdu 2084
#include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
using namespace std; typedef long long LL;
const int INF = (<<)-;
const int mod=;
const int maxn=; int d[][],a[][]; int main(){
int T;
scanf("%d",&T);
while(T--){
int n;
scanf("%d",&n);
memset(d,,sizeof(d));
for(int i = ;i<=n;i++)
for(int j = ;j<=i;j++) scanf("%d",&a[i][j]); for(int i = n;i>=;i--){
for(int j = ;j<=i;j++)
d[i][j] = max(d[i+][j],d[i+][j+]) + a[i][j];
}
printf("%d\n",d[][]);
}
return ;
}
(2)嵌套矩形
把图先建出来,然后d(i) = max(d(j) + 1)
nyoj 16
#include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
using namespace std; typedef long long LL;
const int INF = (<<)-;
const int mod=;
const int maxn=; int g[][];
int d[];
int n; struct node{
int x,y;
}a[maxn]; int dp(int i){
int& ans = d[i];
if(ans > ) return ans;
ans = ;
for(int j = ;j <= n;j++)
if(g[i][j]) ans = max(ans,dp(j) + ); return ans;
} int work(){
int res = -;
for(int i = ;i <= n;i++) res = max(res,dp(i));
return res;
} int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i = ;i <= n;i++) scanf("%d %d",&a[i].x,&a[i].y);
memset(g,,sizeof(g));
memset(d,,sizeof(d)); for(int i = ;i <= n;i++){
for(int j = ;j <= n;j++){
if((a[i].x > a[j].x && a[i].y > a[j].y) || (a[i].x > a[j].y && a[i].y > a[j].x ))
g[i][j] = ;
}
} printf("%d\n",work());
}
return ;
}
Book 动态规划的更多相关文章
- 增强学习(三)----- MDP的动态规划解法
上一篇我们已经说到了,增强学习的目的就是求解马尔可夫决策过程(MDP)的最优策略,使其在任意初始状态下,都能获得最大的Vπ值.(本文不考虑非马尔可夫环境和不完全可观测马尔可夫决策过程(POMDP)中的 ...
- 简单动态规划-LeetCode198
题目:House Robber You are a professional robber planning to rob houses along a street. Each house has ...
- 动态规划 Dynamic Programming
March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...
- 动态规划之最长公共子序列(LCS)
转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...
- C#动态规划查找两个字符串最大子串
//动态规划查找两个字符串最大子串 public static string lcs(string word1, string word2) { ...
- C#递归、动态规划计算斐波那契数列
//递归 public static long recurFib(int num) { if (num < 2) ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- 【BZOJ1700】[Usaco2007 Jan]Problem Solving 解题 动态规划
[BZOJ1700][Usaco2007 Jan]Problem Solving 解题 Description 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地 ...
- POJ 1163 The Triangle(简单动态规划)
http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissi ...
- hdu FatMouse's Speed 动态规划DP
动态规划的解决方法是找到动态转移方程. 题目地址:http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=3§ionid ...
随机推荐
- 洛谷P2822 组合数问题 杨辉三角
没想到这道题竟然这么水- 我们发现m,n都非常小,完全可以O(nm)O(nm)O(nm)预处理出stripe数组,即代表(i,j)(i,j)(i,j) 及其向上的一列的个数,然后进行递推即可. #in ...
- node——模块化
之前写的新闻部分几乎所有操作都写在了一起,这次开始进行模块化. 为什么要模块化: 1.提高开发效率,所有操作在一个文件内,不方便团队操作,模块化可多人同时操作 2.当程序出错,可以分模块寻找错误 3. ...
- github下载报错:Permission denied (publickey). fatal: Could not read from remote repository.
Permission denied (publickey). fatal: Could not read from remote repository. 博主在github上下载tiny face的的 ...
- 自动化测试之firebug、firepath、IDE的使用
1firebug安装-firefox添加组件-firebug 如图 firepath依赖于firebug 展示路径用,安装和firebug一样
- 搞定PHP面试 - 运算符知识点整理
一.算术运算符 1. 概览 例子 名称 结果 $a + $b 加法 $a 和 $b 的和. $a - $b 减法 $a 和 $b 的差. $a * $b 乘法 $a 和 $b 的积. $a / $b ...
- android onContextItemSelected和onMenuItemSelected厉害关系
Android 的activity中onCreateOptionsMenu onMenuItemSelected onOptionsItemSelected onCreateContextMenu o ...
- spring data JPA 中的多属性排序
在此介绍我所用的一种方式: 第一步,引包 import org.springframework.data.domain.Sort;import org.springframework.data.dom ...
- OA项目总结3
struts2自定义标签中 使用in 判断当前值 是否在某个集合中 该属性一方面可以获取前端页面传递过来的参数 另外一个作用就是在数据回显时把用户已经拥有的权限id存入该集合中 放在栈顶 ...
- TensorFlow CNN 测试CIFAR-10数据集
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50738311 1 CIFAR-10 数 ...
- 工具-VS CODE快捷键
快捷键 Ctrl+P 通过文件名或者标签名导航 Ctrl+Tab 在前一个文件和当前文件间切换 F1 打开全局命令 Ctrl+Shift+O will let you navigate ...