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. Emacs org-mode导出html出错

    不知道为什么,当org文件中含有#+TITLE:xxx时,导出会报类似下面的错误: Wrong type argument: listp, #("xxx" 0 3 (:parent ...

  2. DPM 目标检测1

    1. Origin 原始目标检测: HOG梯度模型+目标匹配 为了提过对目标形变的鲁棒性(多视角->多组件): 目标形态多样性—>多个模型 目标的动态变化多视角—> 子模型 目标形变 ...

  3. 下拉选择框QCombox

    下拉列表框样式如图: 字体列表框样式: import sys from PyQt5.QtWidgets import QApplication, QWidget, QComboBox, QFontCo ...

  4. SEO之robots.txt

    [关键词:robot.txt,sitemap,User-Agent,Disallow,Allow][声明:摘自Wikipedia] 1. 定义:robots.txt(统一小写)是一种存放于网站根目录下 ...

  5. [POI2007]ZAP-Queries (莫比乌斯反演+整除分块)

    [POI2007]ZAP-Queries \(solution:\) 唉,数论实在有点烂了,昨天还会的,今天就不会了,周末刚证明的,今天全忘了,还不如早点写好题解. 这题首先我们可以列出来答案就是: ...

  6. Biorhythms HDU - 1370 (中国剩余定理)

    孙子定理: 当前存在三个式子,t%3=2,t%5=3,t%7=2.然后让你求出t的值的一个通解. 具体过程:选取3和5的一个公倍数t1能够使得这个公倍数t1%7==1,然后选取3和7的一个公倍数t2使 ...

  7. mysql 原理 ~ binlog

    一 简介:我们会持续对binlog进行分析,但是不深入代码二 版本 5.6    格式    GTID和传统格式    传统格式     一 binlog针对具体事务注意点-1         1 u ...

  8. python - class内置方法 doc/module/del(析构方法)/cal 方法

    __doc__ # __doc__ #摘要信息 #这个属性不会继承给子类 class Test(): """这是摘要信息""" pass x ...

  9. Django学习手册 - 模板语言(前端获取后台数据)

    先在views视图内,定义列表数据,以及字典数据.运用render函数传递两个列表数据至前端. from django.shortcuts import render list_info = [ {& ...

  10. [转]C++11的enum class & enum struct和enum

    1. 旧版enum存在的问题 问题 描述 1 向整形的隐式转换(Implicit conversion to an integer) 2 无法指定底层所使用的数据类型(Inability to spe ...