动态规划(Dynamic Programming, DP)是一种用来解决一类最优化问题的算法思想,简单来使,动态规划是将一个复杂的问题分解成若干个子问题,或者说若干个阶段,下一个阶段通过上一个阶段的结果来推导出,为了避免重复计算,必须把每阶段的计算结果保存下来,方便下次直接使用。

动态规划有递归和递推两种写法。一个问题必须拥有重叠子问题和最优子结构才能使用动态归来来解决,即一定要能写出一个状态转移方程才能使用动态规划来解决。

最大连续子序列和:

令状态dp[i]表示以A[i]作为末尾的连续序列的最大和,

代码:

 // 最大连续子序列和
#include <stdio.h>
#include <algorithm>
using namespace std; const int maxn = ;
int A[maxn], dp[maxn]; // A[i]存放序列,dp[i]存放以A[i]结尾的连续序列的最大和
int main()
{
freopen("in.txt", "r", stdin);
int n;
scanf("%d", &n);
for (int i = ; i < n; i++){
scanf("%d", &A[i]);
} // 边界
dp[] = A[];
for (int i = ; i < n; i++){
// 状态转移方程
dp[i] = max(dp[i - 1] + A[i], A[i]);
} // dp[i]存放以A[i]结尾的连续序列的最大值,需要遍历 i 得到最大的才是结果
int k = ;
for (int i = ; i < n; i++){
if (dp[i] > dp[k]){
k = i;
}
} printf("%d\n", dp[k]);
fclose(stdin);
return ;
}

题型实战:

              1007 Maximum Subsequence Sum (25分)

Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to be { N​i​​, N​i+1​​, ..., N​j​​ } where 1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.

Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

Input Specification:

Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (≤10000). The second line contains K numbers, separated by a space.

Output Specification:

For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

Sample Input:

10
-10 1 2 3 4 -5 -23 3 7 -21

Sample Output:

10 1 4

代码:

 #include <stdio.h>
#include <algorithm>
using namespace std; const int maxn = ; int A[maxn];
// 两个数组
int firsts[maxn], dp[maxn]; int main()
{
// 读入数据
// freopen("in.txt", "r", stdin);
int n;
scanf("%d", &n);
for (int i = ; i < n; i++){
scanf("%d", &A[i]);
} // 边界
dp[] = A[], firsts[] = ;
// 如果d[i - 1] > 0, firsts[i] = first[i - 1], 否则firsts[i] = i;
for (int i = ; i < n; i++){
if (dp[i - ] >= ){
firsts[i] = firsts[i - ];
dp[i] = dp[i - ] + A[i];
}
else{
firsts[i] = i;
dp[i] = A[i];
}
}
// 遍历dp,寻找最大的子序列和
int k = ;
for (int i = ; i < n; i++){
if (dp[i] > dp[k]){
k = i;
}
} // 输出
if (dp[k] < ){ // 如果最大子序列和都小于0,说明所有元素都是负数
printf("0 %d %d\n", A[], A[n - ]);
}
else{
printf("%d %d %d\n", dp[k], A[firsts[k]], A[k]);
} // fclose(stdin); return ;
}

动态规划(Dynamic Programming, DP)---- 最大连续子序列和的更多相关文章

  1. HDU 4223 Dynamic Programming?(最小连续子序列和的绝对值O(NlogN))

    传送门 Description Dynamic Programming, short for DP, is the favorite of iSea. It is a method for solvi ...

  2. 动态规划-Dynamic Programming(DP)

    动态规划 动态规划方法心得 ​ 动态规划是一般的面试.笔试中的高频算法题,熟练掌握必要的.动态规划的中心思想是在解决当前问题时,可以由之前已经计算所得的结果并结合现在的限制条件递推出结果.由于此前的计 ...

  3. 动态规划(Dynamic Programming)算法与LC实例的理解

    动态规划(Dynamic Programming)算法与LC实例的理解 希望通过写下来自己学习历程的方式帮助自己加深对知识的理解,也帮助其他人更好地学习,少走弯路.也欢迎大家来给我的Github的Le ...

  4. 动态规划 Dynamic Programming 学习笔记

    文章以 CC-BY-SA 方式共享,此说明高于本站内其他说明. 本文尚未完工,但内容足够丰富,故提前发布. 内容包含大量 \(\LaTeX\) 公式,渲染可能需要一些时间,请耐心等待渲染(约 5s). ...

  5. 6专题总结-动态规划dynamic programming

    专题6--动态规划 1.动态规划基础知识 什么情况下可能是动态规划?满足下面三个条件之一:1. Maximum/Minimum -- 最大最小,最长,最短:写程序一般有max/min.2. Yes/N ...

  6. 动态规划Dynamic Programming

    动态规划Dynamic Programming code教你做人:DP其实不算是一种算法,而是一种思想/思路,分阶段决策的思路 理解动态规划: 递归与动态规划的联系与区别 -> 记忆化搜索 -& ...

  7. 动态规划 Dynamic Programming

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

  8. 动态规划系列(零)—— 动态规划(Dynamic Programming)总结

    动态规划三要素:重叠⼦问题.最优⼦结构.状态转移⽅程. 动态规划的三个需要明确的点就是「状态」「选择」和「base case」,对应着回溯算法中走过的「路径」,当前的「选择列表」和「结束条件」. 某种 ...

  9. 最优化问题 Optimization Problems & 动态规划 Dynamic Programming

    2018-01-12 22:50:06 一.优化问题 优化问题用数学的角度来分析就是去求一个函数或者说方程的极大值或者极小值,通常这种优化问题是有约束条件的,所以也被称为约束优化问题. 约束优化问题( ...

随机推荐

  1. 关于map 的几种方式

    java为数据结构中的映射定义了一个接口java.util.Map;它有四个实现类,分别是==HashMap Hashtable LinkedHashMap 和TreeMap.== Map主要用于存储 ...

  2. css3基本选择器+属性选择器+动态伪类+UI状态伪类+结构类

    后代选择器 祖先元素 后代元素{ } 子元素选择器(直接子元素选择器) 父元素>子元素{ } 兄弟选择器 元素+兄弟元素(紧邻该元素之后的下一个兄弟元素) 所有兄弟元素选择器 元素~兄弟元素(该 ...

  3. 制作MySQL RPM安装包Spec

    适用环境: 数据库版本:MySQL 操作系统:CentOS 7 制作思路: 将数据库初始化和配置工作放到安装脚本中方便定制: 1.打包MySQL应用目录 2.不自动生成配置文件 3.不自动生成数据目录 ...

  4. AI数据标注行业面临的5大发展困局丨曼孚科技

    根据艾瑞咨询发布的行业白皮书显示,2018年中国人工智能基础数据服务市场规模为25.86亿元,预计2025年市场规模将突破113亿元,行业年复合增长率达到了23.5%.​ 作为人工智能产业的基石,数据 ...

  5. 吴裕雄--天生自然HADOOP操作实验学习笔记:分布式资源调度系统yarn的安装

    实验目的 复习配置hadoop初始化环境 复习配置hdfs的配置文件 学会配置hadoop的配置文件 了解yarn的原理 实验原理 1.yarn是什么 前面安装好了hdfs文件系统,我们可以根据需求进 ...

  6. centos7虚拟机分配静态IP但是得不到IP、不能上网一种可能的原因和解决办法

    1.首先通过ifconfig查看网卡,发现网卡名称为ens33 2. 在/etc/sysconfig/network-scripts/目录下查看网络配置文件 3. 发现有ifcfg-eth0的配置文件 ...

  7. demo ‘todolist’项目开发

    todolist-site-----------主文件夹 css------------css文件文件夹 header.css---主页面头部样式css section.css---主页面内容样式cs ...

  8. 一维数组、二维数组——Java

    一. 一维数组 1.  数组是相同类型数据的有序集合 相同类型的若干个数据,按照一定先后次序排列组合而成 每个数组元素可以通过一个下标来访问它们 其中,每一个数据称作一个数组元素 2. 数组特点: 其 ...

  9. 【Newtonsoft.Json】json序列化小驼峰格式(属性名首字母小写)

    我是一名 ASP.NET 程序员,专注于 B/S 项目开发.累计文章阅读量超过一千万,我的博客主页地址:https://www.itsvse.com/blog_xzz.html 只需要设置JsonSe ...

  10. python之三目运算符的替代品?

    # 不知曾几何时,你是否也觉得Python的三目运算写起来很麻烦呢?(没有过) # 比如: a, b = 3, 4 c = a if a > b else b d = a if a < b ...