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 ...
随机推荐
- Linux系统编程博客参考
通过看前人的博客更易于把握知识要点 http://www.cnblogs.com/mickole/category/496206.html <Linux系统编程> http://www.c ...
- DNS解析过程详解(转载)
DNS解析过程详解(转载) DNS Domain Name System 域名系统,它就是根据域名查出IP地址. 先说一下DNS的几个基本概念: 一. 根域 就是所谓的“.”,其实我们的网址ww ...
- 记一次vip视频破解过程(爱奇艺 芒果 腾讯 优酷 )
1. 在爱奇艺或者优酷中拿到视频的url地址.此时拿到的是加密地址(也可以直接在牛巴巴里面搜名字然后开f12跟踪路由) 2.进入牛巴巴vip视频解析网站.粘贴拿到的url.点击解析 3.f12在net ...
- selenium的显示等待和隐式等待区别
1.selenium的显示等待 原理:显式等待,就是明确的要等到某个元素的出现或者是某个元素的可点击等条件,等不到,就一直等,除非在规定的时间之内都没找到,那么就跳出Exception.(简而言之:就 ...
- C++基础 (6) 第六天 继承 虚函数 虚继承 多态 虚函数
继承是一种耦合度很强的关系 和父类代码很多都重复的 2 继承的概念 3 继承的概念和推演 语法: class 派生类:访问修饰符 基类 代码: … … 4 继承方式与访问控制权限 相对的说法: 爹派生 ...
- 深入了解Python--元组
1. 对原元组进行插入 2. 元组的嵌套使用 3. for循环使用嵌套元组实例 4. 命名元组避免对分片混淆
- Ubuntu中无法update的解决办法
我输入 sudo apt-get update 出现错误: /etc/apt$ sudo apt-get update Err http://security.ubuntu.com precise-s ...
- easyui获取当前点击对象tabs的title和Index
观察上面打开的tabs选项卡,肯定会有一个目前是被选中状态,而这个状态的class属性也肯定是和其他tabs不一样的,有个class等于tabs-selected的 var title = $('.t ...
- FFT卷积相加模板
struct Complex { double r,i; Complex(double _r,double _i):r(_r),i(_i){} Complex(){} Complex operator ...
- [SQL]存储过程建表
create PROC [dbo].CreateUserTable ( @name NVARCHAR(60) ) AS DECLARE @a NVARCHAR(max) SET @a='create ...