动态规划(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. warning: LF will be replaced by CRLF in

    warning: LF will be replaced by CRLF in analysis/Result.csv. The file will have its original line en ...

  2. css基础-css选择器和css文本样式相关

    css基础-css选择器和css文本样式相关: 使用link链入外部样式,页面加载时会同时加载样式 @import url(“*.css”);使用导入式,页面加载完后,才会加载样式 链接伪类的顺序 : ...

  3. SQL Server无备份误删数据的恢复

    在正式生产数据库中,因为客户现场管理不规范产生了一条错误数据,由于自身睡眠不佳加上客户方言表达,将编号记错,在没有备份的情况下,直接连远程数据库执行了delete操作. 由于备份设置的是每日0点,当天 ...

  4. Vmvare扩展虚拟机磁盘大小

    Vmvare设置好虚拟机的磁盘大小之后,发现磁盘空间不够了,这个时候怎么扩展磁盘的大小呢? 首先,在确保虚拟机关闭的情况下,右键设置,选择硬盘,扩展,这样就可以增加磁盘的大小. 但是由于未进行分区和磁 ...

  5. CVE-2020-1938/CNVD-2020-10487 幽灵猫漏洞

    漏洞描述(后期跟进漏洞分析) Tomcat是由Apache软件基金会属下Jakarta项目开发的Servlet容器,按照Sun Microsystems提供的技术规范,实现了对Servlet和Java ...

  6. STL-queue 队列

    #include <iostream> #include <queue> using namespace std; int main() { // queue也很简单 // p ...

  7. 松软科技课堂:jQuery 效果 - 滑动

    jQuery 滑动方法 通过 jQuery,您可以在元素上创建滑动效果. jQuery 拥有以下滑动方法: slideDown() slideUp() slideToggle() jQuery sli ...

  8. linux系统如何进行录屏

    linux系统如何录屏安装SimpleScreenRecorder 按Ctrl+ALt+T打开终端 添加源: sudo add-apt-repository ppa:maarten-baert/sim ...

  9. 使用faker 生成测试数据

    测试数据生成 faker基础使用 from faker import Faker f=Faker(locale='zh_CN') print(f.name()) address 地址 person 人 ...

  10. .net mvc 多文件上传

    1.input文件上传设置允许选择多个文件,设置属性 multiple即可 <input type="file" multiple="multiple" ...