动态规划(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. 教你如何理解JAVA的I/O类库

    花括号MC(huakuohao-mc):关注JAVA基础编程及大数据,注重经验分享及个人成长. Java 的 I/O 流,说简单也简单,说复杂也复杂.复杂是因为进行一次常规的文件 I/O 操作通常要用 ...

  2. scrapy框架爬取多级页面

    spides.py # -*- coding: utf-8 -*- import scrapy from weather.items import WeatherItem from scrapy.cr ...

  3. excel的count、countif、sunif、if

    一.count统计数值个数 格式:count(指定区域)  , 例如:count(B2:G5) 二.countif统计数值满足条件个数 格式:COUNTIF(条件区域,指定条件)  ,例如:count ...

  4. 吴裕雄--天生自然HADOOP操作实验学习笔记:hbase的shell应用v2.0

    HRegion 当表的大小超过设置值的时候,HBase会自动地将表划分为不同的区域,每个区域包含所有行的一个子集.对用户来说,每个表是一堆数据的集合,靠主键来区分.从物理上来说,一张表被拆分成了多块, ...

  5. HQL查询 HQL Named parameter [xxx] not set 的解决办法

    org.springframework.dao.InvalidDataAccessResourceUsageException: Named parameter [xxx] not set; nest ...

  6. salt 安装 以及salt-api使用

    salt--master    和 salt-minion 控制端 被控制端 通过 salt-api 访问 salt-master  来控制salt-minion 执行 命令  返回结果 LINUX ...

  7. 【35】单层卷积网络(simple convolution)

    今天我们要讲的是如何构建卷积神经网络的卷积层,下面来看个例子.   上节课,我们已经讲了如何通过两个过滤器卷积处理一个三维图像,并输出两个不同的4×4矩阵.假设使用第一个过滤器进行卷积,得到第一个4× ...

  8. openlayers添加弹出框

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. Docker 使用记录

    1.Docker 镜像加载本地镜像 2.Docker 创建镜像: 创建dockerfile 文件: 进入到文件目录下: 输入命令 docker build -t xxxx . 注意:后面的小点要有, ...

  10. PHP正则表达式及表单注册案例

    正则表达式是一种具有特定模式的用来匹配文本的字符串 preg_match 匹配 $pattern = '/php/'; $subject = "php 是最好的编程语言,php 没有之一!& ...