当前位置:网站首页>Nine degrees 1480: maximum ascending subsequence sum (dynamic programming idea for the maximum value)
Nine degrees 1480: maximum ascending subsequence sum (dynamic programming idea for the maximum value)
2022-07-05 10:33:00 【Full stack programmer webmaster】
A sequence of numbers bi, When b1 < b2 < … < bS When , We call this sequence ascending . For a given sequence (a1, a2, …,aN), We can get some ascending subsequences (ai1, ai2, …, aiK), here 1 <= i1 < i2 < … < iK <= N. such as , For the sequence (1, 7, 3, 5, 9, 4, 8), It has some ascending subsequences , Such as (1, 7), (3, 4, 8) wait . The maximum sum of these subsequences is 18, For subsequences (1, 3, 5, 9) And .
Your task , For a given sequence , Find the maximum ascending subsequence and . Be careful , The sum of the longest ascending subsequence is not necessarily the largest , Example sequence (100, 1, 2, 3) The maximum sum of ascending subsequences is 100, The longest ascending subsequence is (1, 2, 3).
Ideas
1.dp[i] Said to i Maximum ascending sequence at the end
dp[i] = max(dp[j]) + value[i]
2. There is no relationship between the longest ascending subsequence and the largest ascending subsequence
3. The idea of this problem is the same as that of the largest continuous subarray
Code
#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; }
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/109896.html Link to the original text :https://javaforall.cn
边栏推荐
- How did automated specification inspection software develop?
- 爬虫(9) - Scrapy框架(1) | Scrapy 异步网络爬虫框架
- [paper reading] ckan: collaborative knowledge aware autonomous network for adviser systems
- The Alipay in place function can't be found, and the Alipay in place function is offline
- Learning note 4 -- Key Technologies of high-precision map (Part 2)
- QT implements JSON parsing
- 如何判断线程池已经执行完所有任务了?
- Apple 5g chip research and development failure? It's too early to get rid of Qualcomm
- > Could not create task ‘:app:MyTest. main()‘. > SourceSet with name ‘main‘ not found. Problem repair
- “军备竞赛”时期的对比学习
猜你喜欢
随机推荐
flex4 和 flex3 combox 下拉框长度的解决办法
Workmanager learning 1
如何写出高质量的代码?
Interview: is bitmap pixel memory allocated in heap memory or native
【SWT组件】内容滚动组件 ScrolledComposite
Detailed explanation of the use of staticlayout
Livedata interview question bank and answers -- 7 consecutive questions in livedata interview~
ByteDance Interviewer: how to calculate the memory size occupied by a picture
微信小程序中,从一个页面跳转到另一个页面后,在返回后发现页面同步滚动了
钉钉、企微、飞书学会赚钱了吗?
[论文阅读] KGAT: Knowledge Graph Attention Network for Recommendation
The horizontally scrolling recycleview displays five and a half on one screen, lower than the average distribution of five
[论文阅读] CKAN: Collaborative Knowledge-aware Atentive Network for Recommender Systems
LiveData 面试题库、解答---LiveData 面试 7 连问~
请问大佬们 有遇到过flink cdc mongdb 执行flinksql 遇到这样的问题的么?
学习笔记6--卫星定位技术(上)
Interview: how does the list duplicate according to the attributes of the object?
《天天数学》连载58:二月二十七日
Click the picture in the mobile browser and the picture will not pop up
橫向滾動的RecycleView一屏顯示五個半,低於五個平均分布