OpenJudge2757:最长上升子序列

题目描述

http://bailian.openjudge.cn/practice/2757

题目思路

  • 最长递增子序列
  • 非递归更好一些

代码

#include <iostream>
using namespace std;
#define N 1001
int dp[N],a[N];
int main() {
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a[i];
    }   
    dp[0]=1;
    for(int i=1;i<n;i++){
        int tmax=1;
        for(int j=0;j<i;j++){
            if(a[i]>a[j]){
                tmax=dp[j]+1>tmax?dp[j]+1:tmax;
            }
        }
        dp[i]=tmax;

    }
    int res=1;
    for(int i=0;i<n;i++){
        res=dp[i]>res?dp[i]:res;
    }
    cout<<res<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/armstrong_rose/article/details/80677319