其他分享
首页 > 其他分享> > AcWing 1016. 最大上升子序列和(线性DP)

AcWing 1016. 最大上升子序列和(线性DP)

作者:互联网

题目链接


题目描述

一个数的序列 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)。

题目模型


题目代码

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 1010;

int n;
int w[N];
int f[N];

int main()
{
    scanf("%d", &n);
    for(int i = 0; i < n; i ++ ) scanf("%d", &w[i]);
    
    int res = 0;
    for(int i = 0; i < n; i ++ )
    {
        f[i] = w[i];
        for(int j = 0; j < i; j ++ )
            if(w[i] > w[j])
                f[i] = max(f[i], f[j] + w[i]);
                
        res = max(res, f[i]);
    }
    
    printf("%d\n", res);
    
    return 0;
}

标签:include,int,res,++,序列,1016,上升,DP,AcWing
来源: https://www.cnblogs.com/esico/p/16139418.html