动态规划(Dynamic Programming, DP)---- 最大连续子序列和
动态规划(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 ;
}
题型实战:
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } 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)---- 最大连续子序列和的更多相关文章
- HDU 4223 Dynamic Programming?(最小连续子序列和的绝对值O(NlogN))
传送门 Description Dynamic Programming, short for DP, is the favorite of iSea. It is a method for solvi ...
- 动态规划-Dynamic Programming(DP)
动态规划 动态规划方法心得 动态规划是一般的面试.笔试中的高频算法题,熟练掌握必要的.动态规划的中心思想是在解决当前问题时,可以由之前已经计算所得的结果并结合现在的限制条件递推出结果.由于此前的计 ...
- 动态规划(Dynamic Programming)算法与LC实例的理解
动态规划(Dynamic Programming)算法与LC实例的理解 希望通过写下来自己学习历程的方式帮助自己加深对知识的理解,也帮助其他人更好地学习,少走弯路.也欢迎大家来给我的Github的Le ...
- 动态规划 Dynamic Programming 学习笔记
文章以 CC-BY-SA 方式共享,此说明高于本站内其他说明. 本文尚未完工,但内容足够丰富,故提前发布. 内容包含大量 \(\LaTeX\) 公式,渲染可能需要一些时间,请耐心等待渲染(约 5s). ...
- 6专题总结-动态规划dynamic programming
专题6--动态规划 1.动态规划基础知识 什么情况下可能是动态规划?满足下面三个条件之一:1. Maximum/Minimum -- 最大最小,最长,最短:写程序一般有max/min.2. Yes/N ...
- 动态规划Dynamic Programming
动态规划Dynamic Programming code教你做人:DP其实不算是一种算法,而是一种思想/思路,分阶段决策的思路 理解动态规划: 递归与动态规划的联系与区别 -> 记忆化搜索 -& ...
- 动态规划 Dynamic Programming
March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...
- 动态规划系列(零)—— 动态规划(Dynamic Programming)总结
动态规划三要素:重叠⼦问题.最优⼦结构.状态转移⽅程. 动态规划的三个需要明确的点就是「状态」「选择」和「base case」,对应着回溯算法中走过的「路径」,当前的「选择列表」和「结束条件」. 某种 ...
- 最优化问题 Optimization Problems & 动态规划 Dynamic Programming
2018-01-12 22:50:06 一.优化问题 优化问题用数学的角度来分析就是去求一个函数或者说方程的极大值或者极小值,通常这种优化问题是有约束条件的,所以也被称为约束优化问题. 约束优化问题( ...
随机推荐
- AGC011-C Squared Graph
题意 给定一个\(n\)个点\(m\)条边的图,构建一个\(n^2\)个点的图,新图的每个点都可以看成一个二元组,新图上的点\((a,b)和(a′,b′)\)之间有边,当且仅当原图中\((a,a′), ...
- Upx 压缩go编译的程序 frp
1. frp 程序占用大 .路由器 不够空间 2. UPX 下载地址 https://github.com/upx/upx/releases/ 3. 压缩命令 upx.exe -9 C ...
- 第十届蓝桥杯CB题目I-分析
思路分析://感谢写文博主 思路:相信大多数人和我一样在比赛的时候把这题想的太简单了_(:з」∠)_ 这题和去年的最后一题很类似,就是分类讨论,去年放在了最后一题,今年在倒数第二题,说明难度不算太难, ...
- Uva12169 扩展欧几里得模板
Uva12169(扩展欧几里得) 题意: 已知 $x_i=(a*x_{i-1}+b) mod 10001$,且告诉你 $x_1,x_3.........x_{2t-1}$, 让你求出其偶数列 解法: ...
- Local changes were not restore
问题是这样的: 更新代码的时候出现这个弹框,不能更新最新代码 解决如下: 直接点击Clear [注意:这个操作是放弃本地所有的修改,如果要找回代码千万不要点击] 再点击Apply Stash 就可以 ...
- 【29】带你了解计算机视觉(Computer vision)
计算机视觉(Computer vision) 计算机视觉是一个飞速发展的一个领域,这多亏了深度学习. 深度学习与计算机视觉可以帮助汽车,查明周围的行人和汽车,并帮助汽车避开它们. 还使得人脸识别技术变 ...
- openlayers编辑区域
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- liner-classifiers-SVM
1支持向量机 参考看了这篇文章你还不懂SVM你就来打我 第一遍看完确实有想打死作者的冲动,但是多看几遍之后,真香~ [SVM---这可能是最直白的推导了] 个人觉得这篇文章讲的很清楚,条理清晰,数学推 ...
- Docker最全教程——从理论到实战(十八)
前言 VS Code是一个年轻的编辑器,但是确实是非常犀利.通过本篇,老司机带你使用VS Code玩转Docker——相信阅读本篇之后,无论是初学者还是老手,都可以非常方便的玩转Docker了!所谓是 ...
- java开发就业招聘管理系统 ssh源码
开发环境: Windows操作系统开发工具: MyEclipse+Jdk+Tomcat+MySql数据库 此项目分为 用户 企业 管理员三种角色 运行效果图