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)

 1 #include<iostream>
 2 #include<algorithm>
 3 #define INF 32767
 4 using namespace std;
 5
 6 int dp[100];
 7 int coin[3] = { 1, 2, 3 };
 8
 9 int main()
10 {
11     int sum;
12     cin >> sum;
13     dp[0] = 0;
14     for (int i = 1; i <= 100; ++i)
15         dp[i] = INF;
16     for (int i = 1; i <= sum; ++i)
17         for (int j = 0; j <= 2; ++j)
18             if (coin[j] <= i)
19                 dp[i] = min(dp[i], dp[i - coin[j]] + 1);
20     cout << dp[sum] << endl;
21     return 0;
22 }

2. 最长递增子序列

• 题目描述:最长递增子序列(Longest Increasing Subsequence)是指找到一个给定序列的最长子序列的长度,使得子序列中的所有元素单调递增。

给定一个序列,求解它的最长 递增 子序列 的长度。比如: arr[] = {3,1,4,1,5,9,2,6,5}   的最长递增子序列长度为4。即为:1,4,5,9

 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4
 5 int arr[9] = { 3, 1, 4, 1, 5, 9, 2, 6, 5 };
 6 int dp[9];
 7
 8 int main()
 9 {
10     for (int i = 0; i < 9; ++i)
11         dp[i] = 1;
12     for (int i = 1; i < 9; ++i)
13         for (int j = 0; j < i; ++j)
14             if (arr[i] > arr[j])
15                 dp[i] = max(dp[i], dp[j] + 1);
16     int mi = 0;
17     for (int i = 0; i < 6; ++i)
18         mi = max(mi, dp[i]);
19     cout << mi << endl;
20     return 0;
21 }

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
15
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
Sample Output
30
Problem Source
IOI 1994

代码:

 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4
 5 int dp[100][100];
 6 int arr[100][100];
 7
 8 int main()
 9 {
10     int N;
11     cin >> N;
12     if (N == 1)
13         cout << N;
14     for (int i = 0; i < N; ++i)
15         for (int j = 0; j <= i; ++j)
16             cin >> arr[i][j];
17     for (int i = 0; i < N; ++i)
18         dp[N - 1][i] = arr[N - 1][i];
19     for (int i = N - 2; i >= 0; --i)
20         for (int j = 0; j <= i; ++j)
21             dp[i][j] = max(arr[i][j] + dp[i + 1][j], arr[i][j] + dp[i + 1][j + 1]);
22     cout << dp[0][0] << endl;
23     return 0;
24 }

4. 最大最大连续子序列和/积

• 求取数组中最大连续子序列和,例如给定数组为A={1, 3, -2, 4, -5}, 则最大连续子序列和为6,即1+3+(-2)+ 4 = 6。

• 求取数组中最大连续子序列积。

参考资料

• 常见动态规划问题分析与求解
• 关于序列的面试题2------------最大连续子序列和以及积

转载于:https://www.cnblogs.com/sunbines/p/9011149.html

【动态规划】Part1相关推荐

  1. 蓝桥杯备考-刷题之路-动态规划算法(DP算法)Part1

    之前在刷力扣的时候就是浑浑噩噩的,照着评论区的答案写了一遍就万事大吉了,没有深度思考过.这次备考蓝桥杯看到DP算法的第一道题就不会,更难受的是看答案了依然完全不理解,所以决心把DP算法一次弄懂. 开始 ...

  2. 彻底搞懂-扔鸡蛋问题-方程-动态规划

    1.题目: 2个鸡蛋,从100层楼上往下扔,以此来测试鸡蛋的硬度,比如鸡蛋在第9层没有摔碎而在第10层摔碎了,那么鸡蛋不会摔碎的零界点就是9层,如何用最少的尝试次数,测试出鸡蛋不会摔碎的临界点? 2. ...

  3. 学习记录 动态规划实时更新

    这里是目录 写在读前: Part1 前景知识: Part2 具体模型: 简单模型: 简单递推与数学:[P1077 [NOIP2012 普及组] 摆花](https://www.luogu.com.cn ...

  4. 伍六七带你学算法 动态规划 ——不同路径

    力扣 62. 不同路径 难度 中等 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格 ...

  5. 由动态规划计算编辑距离引发的思考

    简单介绍 编辑距离算法: https://www.cnblogs.com/BlackStorm/p/5400809.html https://wizardforcel.gitbooks.io/the- ...

  6. LeetCode 10. Regular Expression Matching python特性、动态规划、递归

    前言 本文主要提供三种不同的解法,分别是利用python的特性.动态规划.递归方法解决这个问题 使用python正则属性 import reclass Solution2:# @return a bo ...

  7. 2016.4.2 动态规划练习--讲课整理

    1.codevs1742 爬楼梯  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 小明家外面有一个长长的楼梯,共N阶.小明的腿 ...

  8. 算法设计与分析第4章 动态规划(二)【DP序列问题】

    第3章 动态规划(二)[DP序列问题] 3.2 DP序列问题 (51nod的动态规划教程很不错,讲解很详细,以下分析来自51nod) 1.矩阵取数问题 给定一个m行n列的矩阵,矩阵每个元素是一个正整数 ...

  9. 算法设计与分析第4章 动态规划(一)【背包问题】

    第3章动态规划(一)[背包问题] 基本思想: 动态规划算法与分治法类似,其基本思想也是将待求解问题分解成若干个子问题,但是经分解得到的子问题往往不是互相独立的.不同子问题的数目常常只有多项式量级.在用 ...

最新文章

  1. Hibernate的批量查询
  2. lisp画垫圈_晓东CAD家园-论坛-LISP/VLISP程序库-[LISP程序]:俺的画内六角圆柱头螺钉的LISP程序-见附件 - Powered by Discuz!...
  3. 【python】mysql的操作
  4. 【51项目】电子密码锁设计
  5. java环境变量的配置_一文带你学会Java环境变量配置(小白向)
  6. 三网 —— 计算机网络、电信网络、广播电视网络(移动网络)
  7. CentOS7下Tomcat启动慢的原因及解决方案
  8. [Hyper-V]使用操作系统模板创建新的虚拟机
  9. protues软件仿真-LCD1602
  10. 华为手机的分类有何区别_华为手机有多少种型号,几个系列?
  11. HTML美化页面(下)
  12. CDA LEVEL I分数占比解读,看懂这些,考试更容易拿分
  13. ceph osd 修复备忘
  14. EcmaScript 2022中的新特性
  15. c当中extern详解
  16. 不登陆路由器查询路由器IP地址和物理地址
  17. android 9 qq登录,天天练qq登录版
  18. Daimayuan Online Judge 上帝的集合
  19. vins estimator ProjectionFactor (Td) factor
  20. 电子发票全流程电子化管理指南-摘要

热门文章

  1. 伍六七带你学算法 入门篇-最长回文串
  2. 从风投看中国IT行业的发展
  3. docker 容器访问宿主机的解决方式
  4. Go语言环境搭建(Windows+Linux)
  5. 新一代图像AI ISP技术
  6. AI框架外部用户贡献代码
  7. 什么是OpenMAX技术分析OpenMAX
  8. 2021年大数据常用语言Scala(九):基础语法学习 break和continue
  9. Java基础语法运算和控制符
  10. DCN-2655 ssh 远程登陆配置