hdu 1087 LIS 最长递增子序列的值

本文介绍了一道关于最长递增子序列(LIS)的经典算法题,该问题旨在求解给定序列中满足特定递增条件的最大值。通过动态规划的方法解决了这一问题,并给出了完整的C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Input
Input contains multiple test cases. Each test case is described in a line as follow:
N value_1 value_2 …value_N
It is guarantied that N is not more than 1000 and all value_i are in the range of 32-int.
A test case starting with 0 terminates the input and this test case is not to be processed.

Output
For each case, print the maximum according to rules, and one line one case.

Sample Input
3 1 3 2
4 1 2 3 4
4 3 3 2 1
0

Sample Output
4
10
3

题解:

一道LIS的水题,注意求的是值,不是长度,模板上都是求的是长度。

代码:

#include <bits/stdc++.h>

using namespace std;
const int maxn = 1000+10;
int a[maxn]={0};
int dp[maxn];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0) break;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }

        memset(dp,0,sizeof(dp));

        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<i;j++)
            {
                if(a[i]>a[j])
                {
                    dp[i]=max(dp[i],dp[j]+a[i]);
                }
            }
        }

        int* pos = max_element(dp+1,dp+n+1);
        cout<<*pos<<endl;

    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值