POJ-2533(最长上升子序列(简单dp))

题目名称:Longest Ordered Subsequence 
题目链接:https://vjudge.net/problem/POJ-2533
简单的动态规划, 状态转移方程为:
dp[i] = max(dp[i], dp[j] + 1) 当 a[j] > a[i] && j < i;
#include 
#include 
#include 
#include 

using namespace std;

const int MAXN = 1005;
int a[MAXN], dp[MAXN];

int main()
{
    int n;
    while(scanf("%d", &n) != EOF)
    {
        for(int i = 0; i < n; ++i) scanf("%d", &a[i]);

        //dp
        int res = -1;
        for(int i = 0; i < n; ++i)
        {
            dp[i] = 1;
            for(int j = 0; j < i; ++j)
                if(a[j] < a[i]) dp[i] = max(dp[i], dp[j]+1);
            res = max(res, dp[i]);
        }
        printf("%d\n", res);
    }
    return 0;
}

你可能感兴趣的:(动规)