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
7

3 8

8 1 0

2 7 4 4

4 5 2 6 5 (Figure 1)

Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a route that starts at the top and ends somewhere on the base. Each step can go either diagonally down to the left or diagonally down to the right.

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
7
3 8
8 1 0
2 7 4 4
4 5 2 6 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的更多相关文章

  1. 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 服务器安装操 ...

  2. 增强学习(三)----- MDP的动态规划解法

    上一篇我们已经说到了,增强学习的目的就是求解马尔可夫决策过程(MDP)的最优策略,使其在任意初始状态下,都能获得最大的Vπ值.(本文不考虑非马尔可夫环境和不完全可观测马尔可夫决策过程(POMDP)中的 ...

  3. 简单动态规划-LeetCode198

    题目:House Robber You are a professional robber planning to rob houses along a street. Each house has ...

  4. Linux平台 Oracle 11gR2 RAC安装Part1:准备工作

    一.实施前期准备工作 1.1 服务器安装操作系统 1.2 Oracle安装介质 1.3 共享存储规划 1.4 网络规范分配 二.安装前期准备工作 2.1 各节点系统时间校对 2.2 各节点关闭防火墙和 ...

  5. 动态规划 Dynamic Programming

    March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...

  6. 动态规划之最长公共子序列(LCS)

    转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...

  7. C#动态规划查找两个字符串最大子串

     //动态规划查找两个字符串最大子串         public static string lcs(string word1, string word2)         {            ...

  8. C#递归、动态规划计算斐波那契数列

    //递归         public static long recurFib(int num)         {             if (num < 2)              ...

  9. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

随机推荐

  1. CodeBblock 常用快捷键 (最常用)

    ==日常编辑== • 按住Ctrl滚滚轮,代码的字体会随你心意变大变小. • 在编辑区按住右键可拖动代码,省去拉(尤其是横向)滚动条之麻烦:相关设置:Mouse Drag Scrolling. • C ...

  2. DOM-Document对象

     一. 整体介绍   这里介绍DOM对象中的Document对象. 何为Document对象?每个载入浏览器的HTML文档都会成为Document对象,Document对象可以帮助我们对所有的HTML ...

  3. 用命令行发布android程序

    在开发android程序的过程中,我们使用ant debug和ant installd这两个命令就够了,不涉及到APK的签名. 但是在正式发布我们的Android程序时,需要对APK签名.ant re ...

  4. Linux - rar 压缩

    Linux - rar yum -y install libstdc++.so. wget http://rarsoft.com/rar/rarlinux-4.0.1.tar.gz cd rar ma ...

  5. Nginx 中 FastCGI 配置示例

    nginx 中 FastCGI 参数:主要是在 http 层 :保证PHP环境的高校运行 主要对PHP用来解析 fastcgi_cache_path /tmp/fastcgi_cache levels ...

  6. comfirm和prompt的区别

    comfirm和prompt的区别. <html> <title>测试页面</title> <head> </head> <body& ...

  7. Mac下配置多个SSH KEY访问远程Git服务

    第一步 生成对应的ssh key 1 后面输入你的用户名 或者 邮箱 2 输入一个独立的ssh key名字 区别之前的名字 第二步  编辑 config文件 在.ssh/目录下面 在config文件配 ...

  8. logback配置按天产生日志文件

    1 依赖Jar包 pom配置 也可以根据自己的版本来 <dependency> <groupId>org.slf4j</groupId> <artifactI ...

  9. POJ 1200 Crazy Search (哈希)

    题目链接 Description Many people like to solve hard puzzles some of which may lead them to madness. One ...

  10. 使用flask_socketio实现服务端向客户端定时推送

    websocket连接是客户端与服务器之间永久的双向通信通道,直到某方断开连接. 双向通道意味着在连接时,服务端随时可以发送消息给客户端,反之亦然,这在一些需要即时通讯的场景比如多人聊天室非常重要. ...