动态规划(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. jQuery---事件的执行顺序

    事件的执行顺序 // 1 这个是p自己注册的事件(简单事件) $("p").on("click", function () { alert("呵呵哒& ...

  2. CF round 623 Div.1D Tourism 题解

    题目链接:https://codeforces.com/contest/1314/problem/D 大意: \(n\) 个顶点的有向图,顶点编号为 \(1\) 到 \(n\),任意两个不同的顶点 \ ...

  3. JS格式时间

    Date.prototype.format = function(format) { var o = { "M+": this.getMonth() + 1, //month &q ...

  4. JS Radio结合TEXT

    <script> function fun_a(value){ if(value === "on"){ document.getElementById('a').dis ...

  5. #4864. [BeiJing 2017 Wc]神秘物质 [FHQ Treap]

    这题其实挺简单的,有个东西可能稍微难维护了一点点.. \(merge\ x\ e\) 当前第 \(x\) 个原子和第 \(x+1\) 个原子合并,得到能量为 \(e\) 的新原子: \(insert\ ...

  6. ElementUI的Table表格添加自定义头CheckBox多选框

    在ElmentUI的Table表格组件中,也许你会使用type为selection值的多选框功能,但是此时设置的label属性不生效,不能设置标题名称:有时候我们的需求就是要添加标题名称,那该如何处理 ...

  7. Ubuntu在当前用户目录下安装python 包

    对于tar.gz文件: tar -zxvf setuptools-19.6.tar.gz cd setuptools-19.6.tar.gz python3 setup.py build python ...

  8. 收录了老师发的几个 download ebook and paper 的 webpage

    Library Genesis (important) http://zh.b-ok.org National Academic Press OpenStax CNX gen.lib.rus.ec l ...

  9. PAT (Advanced Level) Practice 1035 Password (20 分)

    To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem ...

  10. 无法解析的外部符号 _snprintf

    VS2010下: 在使用第三方静态库 遇到无法解析的外部符号 _snprintf . 编译第三方库的时候 看到有 warning C4013: 'snprintf' undefined; assumi ...