【动态规划】Part1
1. 硬币找零
题目描述:假设有几种硬币,如1、3、5,并且数量无限。请找出能够组成某个数目的找零所使用最少的硬币数。
分析: dp [0] = 0
dp [1] = 1 + dp [1-1]
dp [2] = 1 + dp [2-1]
dp [3] = min (dp [3 - 1] + 1, dp [3 - 3] + 1)
#include<iostream>
#include<algorithm>
#define INF 32767
using namespace std; int dp[];
int coin[] = { , , }; int main()
{
int sum;
cin >> sum;
dp[] = ;
for (int i = ; i <= ; ++i)
dp[i] = INF;
for (int i = ; i <= sum; ++i)
for (int j = ; j <= ; ++j)
if (coin[j] <= i)
dp[i] = min(dp[i], dp[i - coin[j]] + );
cout << dp[sum] << endl;
return ;
}
2. 最长递增子序列
• 题目描述:最长递增子序列(Longest Increasing Subsequence)是指找到一个给定序列的最长子序列的长度,使得子序列中的所有元素单调递增。
给定一个序列,求解它的最长 递增 子序列 的长度。比如: arr[] = {3,1,4,1,5,9,2,6,5} 的最长递增子序列长度为4。即为:1,4,5,9
#include<iostream>
#include<algorithm>
using namespace std; int arr[] = { , , , , , , , , };
int dp[]; int main()
{
for (int i = ; i < ; ++i)
dp[i] = ;
for (int i = ; i < ; ++i)
for (int j = ; j < i; ++j)
if (arr[i] > arr[j])
dp[i] = max(dp[i], dp[j] + );
int mi = ;
for (int i = ; i < ; ++i)
mi = max(mi, dp[i]);
cout << mi << endl;
return ;
}
3. 数字三角形
Problem description | |
|
|
Input | |
Your program is to read from standard input. The first line contains one integer T, the number of test cases, for each test case: the first line contain a 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 for each test case one line. | |
Sample Input | |
1 5 |
|
Sample Output | |
30 |
|
Problem Source | |
IOI 1994 |
代码:
#include<iostream>
#include<algorithm>
using namespace std; int dp[][];
int arr[][]; int main()
{
int N;
cin >> N;
if (N == )
cout << N;
for (int i = ; i < N; ++i)
for (int j = ; j <= i; ++j)
cin >> arr[i][j];
for (int i = ; i < N; ++i)
dp[N - ][i] = arr[N - ][i];
for (int i = N - ; i >= ; --i)
for (int j = ; j <= i; ++j)
dp[i][j] = max(arr[i][j] + dp[i + ][j], arr[i][j] + dp[i + ][j + ]);
cout << dp[][] << endl;
return ;
}
4. 最大最大连续子序列和/积
• 求取数组中最大连续子序列和,例如给定数组为A={1, 3, -2, 4, -5}, 则最大连续子序列和为6,即1+3+(-2)+ 4 = 6。
• 求取数组中最大连续子序列积。
参考资料
• 常见动态规划问题分析与求解• 关于序列的面试题2------------最大连续子序列和以及积
【动态规划】Part1的更多相关文章
- Linux平台 Oracle 10gR2(10.2.0.5)RAC安装 Part1:准备工作
Linux平台 Oracle 10gR2(10.2.0.5)RAC安装 Part1:准备工作 环境:OEL 5.7 + Oracle 10.2.0.5 RAC 1.实施前准备工作 1.1 服务器安装操 ...
- 增强学习(三)----- MDP的动态规划解法
上一篇我们已经说到了,增强学习的目的就是求解马尔可夫决策过程(MDP)的最优策略,使其在任意初始状态下,都能获得最大的Vπ值.(本文不考虑非马尔可夫环境和不完全可观测马尔可夫决策过程(POMDP)中的 ...
- 简单动态规划-LeetCode198
题目:House Robber You are a professional robber planning to rob houses along a street. Each house has ...
- Linux平台 Oracle 11gR2 RAC安装Part1:准备工作
一.实施前期准备工作 1.1 服务器安装操作系统 1.2 Oracle安装介质 1.3 共享存储规划 1.4 网络规范分配 二.安装前期准备工作 2.1 各节点系统时间校对 2.2 各节点关闭防火墙和 ...
- 动态规划 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中都出现过并且出现顺序与 ...
随机推荐
- Linux命令(九)查找文件find
4. find -type f 文件 d 目录 l 连接 5. find -size 6. find -maxdepth -2 (最深路径为2) 7. find ./ -maxdepth 1 ...
- 转---python os.exec*()家族函数的用法
execl(file, arg0,arg1,...) 用参数列表arg0, arg1 等等执行文件 execv(file, arglist) 除了使用参数向量列表,其他的和execl()相同 exec ...
- mysql中sql语句的常用语句
1:提取公共的sql语句: 2:动态添加----sql语句: 代码: <insert id="test1" parameterType="com.floor.sho ...
- string和list互转
import string str = 'abcde' list = list(str) OR list = str.split() list ['a', 'b', 'c', 'd', 'e'] st ...
- VS复制文件到输出目录
1.选中项目文件 2. 3.编译时就会自动创建目录,并复制文件
- luogu 1064 金明的预算方案
01背包 变形,有主附件的背包内则更改决策 original: 1) 不选 2)选,f[j-w[i]]+v[i] now : 1)不选 2)选主 3)主 附1 4)主 附2 ...
- android AsyncHttpClient使用
1.www.github.com下载jar包 loopj/android-async-http 将下载好的文件导入项目中 2.main.xml <?xml version="1.0&q ...
- luogu P4360 [CEOI2004]锯木厂选址
斜率优化dp板子题[迫真] 这里从下往上标记\(1-n\)号点 记\(a_i\)表示前缀\(i\)里面树木的总重量,\(l_i\)表示\(i\)到最下面的距离,\(s_i\)表示\(1\)到\(i-1 ...
- idea的起步配置
工欲善其事,必先利其器 1.安装 https://www.jetbrains.com/idea/download/#section=windows 可以选择不同平台的安装包,版本一般Ultimate, ...
- OGG实现两台Oracle数据库的同步
今天通过最简单的一个例子,给大家讲解下 goldengate 实现两台Oracle数据库的同步.内容如下:1.配置数据库信息.2.安装golden gate.3.配置golden gate.4.测试同 ...