Book 动态规划
2024-10-01 12:12:19
虽然之前学过一点点,但是还是不会------现在好好跟着白书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 ;
}
最新文章
- 原生ajax基础
- MapReduce实现WordCount
- Java与mysql数据库编程中遇见“Before start of result set at com.mysql.jdbc.SQLError.createSQLException” 的解决办法
- ORACLE 数据的逻辑组成
- tableView 显示区域偏移
- 关于session更新的问题
- 对于JavaScript对象的prototype和__proto__的理解
- HashMap的使用方法及注意事项
- javascript中,数组常用的方法有哪些?
- 对";一维最大子数组和";问题的思考
- System.Text.RegularExpressions.Regex
- 前端综合学习笔记---异步、ES6/7、Module、Promise同步 vs 异步
- IntelliJ IDEA下Cannot resolve symbol XXX的解决方法
- Net Core平台灵活简单的日志记录框架NLog+Mysql组合初体验
- ionic3 出现莫名广告
- 关于h5使用bpmn.js
- JSP内置对象——Exception对象
- 加速cin的技巧
- python day09作业答案
- java向数据库插入N条数据
热门文章
- Lazy Stored Properties--无括号时为匿名函数
- Java中数组的定义方式
- C# word生成html
- Php+Redis队列原理
- kali2018.2安装配置OpenVAS-9及错误处置
- Python编码显示中文乱码
- Untiy中的数据平滑处理
- [置顶]
 大数据架构hadoop
- Warning: The following processes: -cmd.exe -java.exe are locking the following directory:
- css 超出宽度出现省略号