当前位置:网站首页>九度 1480:最大上升子序列和(动态规划思想求最值)
九度 1480:最大上升子序列和(动态规划思想求最值)
2022-07-05 10:13:00 【全栈程序员站长】
一个数的序列bi,当b1 < b2 < … < bS的时候,我们称这个序列是上升的。对于给定的一个序列(a1, a2, …,aN),我们可以得到一些上升的子序列(ai1, ai2, …, aiK),这里1 <= i1 < i2 < … < iK <= N。比如,对于序列(1, 7, 3, 5, 9, 4, 8),有它的一些上升子序列,如(1, 7), (3, 4, 8)等等。这些子序列中序列和最大为18,为子序列(1, 3, 5, 9)的和.
你的任务,就是对于给定的序列,求出最大上升子序列和。注意,最长的上升子序列的和不一定是最大的,比如序列(100, 1, 2, 3)的最大上升子序列和为100,而最长上升子序列为(1, 2, 3)。
思路
1.dp[i] 表示以 i 结尾的最大上升序列
dp[i] = max(dp[j]) + value[i]
2. 最长上升子序列和最大上升子序列没有关系
3. 这题的思路和最大连续子数组一样
代码
#include <iostream> #include <stdio.h>
using namespace std; int dp[1001]; int val[1001]; int n; int main() { while(scanf("%d", &n) != EOF ) { for(int i = 0; i < n; i ++) scanf("%d", val+i); dp[0] = val[0]; for(int i = 1; i < n; i ++) { dp[i] = val[i]; for(int j = 0; j < i; j ++) { if(val[j] >= val[i]) continue; dp[i] = max(dp[i], dp[j]+val[i]); } } int resval = 0; for(int i = 0; i < n; i ++) resval = max(resval, dp[i]); cout << resval << endl; } return 0; }
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/109896.html原文链接:https://javaforall.cn
边栏推荐
- How does redis implement multiple zones?
- 《微信小程序-基础篇》小程序中的事件与冒泡
- php解决redis的缓存雪崩,缓存穿透,缓存击穿的问题
- How to write high-quality code?
- A large number of virtual anchors in station B were collectively forced to refund: revenue evaporated, but they still owe station B; Jobs was posthumously awarded the U.S. presidential medal of freedo
- Using directive in angualr2 to realize that the picture size changes with the window size
- 微信小程序中,从一个页面跳转到另一个页面后,在返回后发现页面同步滚动了
- Workmanager learning 1
- How did automated specification inspection software develop?
- Flink CDC cannot monitor MySQL logs. Have you ever encountered this problem?
猜你喜欢
AtCoder Beginner Contest 258「ABCDEFG」
Swift tableview style (I) system basic
Window下线程与线程同步总结
学习笔记6--卫星定位技术(上)
ConstraintLayout的流式布局Flow
手机厂商“互卷”之年:“机海战术”失灵,“慢节奏”打法崛起
Constrained layout flow
【 conseils 】 obtenir les valeurs des axes X et y de la fonction cdfplot dans MATLAB
Who is the "conscience" domestic brand?
How did automated specification inspection software develop?
随机推荐
【 conseils 】 obtenir les valeurs des axes X et y de la fonction cdfplot dans MATLAB
Swift saves an array of class objects with userdefaults and nssecurecoding
Detailed explanation of the use of staticlayout
Write double click event
Excerpt from "sword comes" (VII)
Dedecms website building tutorial
Unity particle special effects series - the poison spray preform is ready, and the unitypackage package is directly used - on
Should the dependency given by the official website be Flink SQL connector MySQL CDC, with dependency added
Workmanager learning 1
To bring Euler's innovation to the world, SUSE should be the guide
【黑马早报】罗永浩回应调侃东方甄选;董卿丈夫密春雷被执行超7亿;吉利正式收购魅族;华为发布问界M7;豆瓣为周杰伦专辑提前开分道歉...
Constraintlayout officially provides rounded imagefilterview
Universal double button or single button pop-up
WorkManager的学习二
How to write high-quality code?
Livedata interview question bank and answers -- 7 consecutive questions in livedata interview~
Interview: is bitmap pixel memory allocated in heap memory or native
MySQL digital type learning notes
AtCoder Beginner Contest 258「ABCDEFG」
面试:List 如何根据对象的属性去重?