hdu 5087 Revenge of LIS II | dp 次长上升子序列

来源:互联网 发布:阿里云注销备案 编辑:程序博客网 时间:2024/05/21 02:49

题意:

找出次长上升子序列的长度。

思路:

进爷叫我去做一下,没想到被坑了好久。一直在想方法,然后又推翻了= =, 看完题解,之前想的一种思路是对的。可但是,但可是,没去实现 囧。

dp[i]:记录以a[i]结尾的最长上升序列的长度。

cot[i]:记录以a[i]结尾的最长长度的达成方案数。(最大为2即可,再多也没用)

code:

#include <cstdlib>#include <cstdio>#include <iostream>#include <algorithm>using namespace std;const int MAXN = 1005;int a[MAXN], dp[MAXN];int cot[MAXN];int main(){    int T;    scanf("%d", &T);    while(T--)    {        int n;        scanf("%d", &n);        for(int i = 1;i <= n; i++)        {            scanf("%d" ,&a[i]);        }        memset(dp, 0, sizeof(dp));        memset(cot, 0, sizeof(cot));        int res = 1;        for(int i = 1;i <= n; i++)        {            dp[i] = 1;            cot[i] = 1;            for(int j = 1;j < i; j++)            {                if(a[i] > a[j])                {                    if(dp[i] == dp[j] + 1)                    {                        cot[i] = 2;                    }                    else if(dp[i] < dp[j] + 1)                    {                        dp[i] = dp[j] + 1;                        cot[i] = cot[j];                    }                }            }            res = max(res, dp[i]);        }        int cc = 0;        for(int i = 1;i <= n; i++)            if(dp[i] == res) cc += cot[i];        printf("%d\n", cc >= 2 ? res : res-1);    }    return 0;}




0 0