HDU - 5087 Revenge of LIS II

来源:互联网 发布:下载风行网络电影 编辑:程序博客网 时间:2024/06/06 04:06

在LIS的O(n^2)的递推过程中,用一个mark[ i ]标记以arr[ i ]结尾的最长上升子序列的长度是否有多条路径推得。



#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>using namespace std;int arr[1011], mark[1011], d[1011];int n;int main(){    int T;    cin>>T;    while(T--)    {        cin>>n;        for(int i=0; i<n; i++)            cin>>arr[i];        memset(mark, 0, sizeof(mark));        for(int i=0; i<n; i++)        {            d[i]=1;            for(int j=0; j<i; j++)                if(arr[i]>arr[j])                {                    if(d[i]==d[j]+1)                        mark[i]=2;                    else if(d[i]<d[j]+1)                    {                        d[i]=d[j]+1;                        mark[i]=mark[j];                    }                }        }        int maxlen=0;        for(int i=0; i<n; i++)            maxlen=max(maxlen, d[i]);        int times=0, num;        for(int i=0; i<n; i++)            if(d[i]==maxlen)            {                times++;                num=i;            }        if(times>=2 || (times==1 && mark[num]>=2))            printf("%d\n", maxlen);        else printf("%d\n", maxlen-1);    }    return 0;}

0 0
原创粉丝点击