POJ-2533 裸LIS

本文介绍了一种求解最长递增子序列问题的动态规划算法,并提供了完整的C++实现代码。该算法通过递归方式计算每个元素为结尾时的最长递增子序列长度,并最终得出整个序列的最长递增子序列长度。

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

紫书上的模板还真是好套呢,我的代码算是O(n3)的复杂度吧,不不,还是个O(n3)的算法

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
#define DEBUG
const int maxn=1000+5,maxv=26,INF=0x3f3f3f3f;
int n,d[maxn],a[maxn];
int dp(int i){
    int& ans=d[i];
    if(d[i]>0)return ans;
    ans=1;
    for(int j=i+1;j<=n;j++){
        if(a[j]>a[i]) ans=max(ans,dp(j)+1);
    }
    return ans;
}
int main(){
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    int ans=0;
    for(int i=1;i<=n;i++){
        ans=max(ans,dp(i));
    }
    printf("%d\n",ans );
#ifdef DEBUG
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值