码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 5087 Revenge of LIS II lcs变形

时间:2014-11-06 22:04:56      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:acm   dp   lcs   

点击打开链接链接 

Revenge of LIS II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1028    Accepted Submission(s): 334


Problem Description
In computer science, the longest increasing subsequence problem is to find a subsequence of a given sequence in which the subsequence‘s elements are in sorted order, lowest to highest, and in which the subsequence is as long as possible. This subsequence is not necessarily contiguous, or unique.
---Wikipedia

Today, LIS takes revenge on you, again. You mission is not calculating the length of longest increasing subsequence, but the length of the second longest increasing subsequence.
Two subsequence is different if and only they have different length, or have at least one different element index in the same place. And second longest increasing subsequence of sequence S indicates the second largest one while sorting all the increasing subsequences of S by its length.
 

Input
The first line contains a single integer T, indicating the number of test cases. 

Each test case begins with an integer N, indicating the length of the sequence. Then N integer Ai follows, indicating the sequence.

[Technical Specification]
1. 1 <= T <= 100
2. 2 <= N <= 1000
3. 1 <= Ai <= 1 000 000 000
 

Output
For each test case, output the length of the second longest increasing subsequence.
 

Sample Input
3 2 1 1 4 1 2 3 4 5 1 1 2 2 2
 

Sample Output
1 3 2
Hint
For the first sequence, there are two increasing subsequence: [1], [1]. So the length of the second longest increasing subsequence is also 1, same with the length of LIS.
 

求出第二长的lcs 

普通lcs再sort是不对的

反例 :

1 1 2

方法一:

统计到每个长度的个数

#include<cstdio>
#include<cstring>
int max(int a,int b)
{
    return a>b?a:b;
}
long long cnt[1111];
int a[1111],dp[1111];
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(int i=1;i<=n;i++)
            cnt[i]=1;
        int ans=0;
        long long num=0;
        for(int i=1;i<=n;i++)
        {
            cnt[i]=1;
            dp[i]=1;
            for(int j=1;j<i;j++)
            {
                if(a[j]<a[i])
                {
                    if(dp[i]<dp[j]+1)
                    {
                        dp[i]=dp[j]+1;
                        cnt[i]=cnt[j];
                    }
                    else if(dp[i]==dp[j]+1)
                        cnt[i]+=cnt[j];
                }
            }
            ans=max(ans,dp[i]);
        }
        for(int i=1;i<=n;i++)
        {
            if(dp[i]==ans)
                num+=cnt[i];
        }
        if(num==1)
            printf("%d\n",ans-1);
        else if(num>1)
            printf("%d\n",ans);
    }
    return 0;
}

方法二:

求lcs的第K优解 开二维的dp

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int dp[1111][3];
int a[1111];
int ans[2222];
int main()
{
    int t,n;
    int s[10];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        memset(dp,0,sizeof(dp));
        int xx=0;
        for(int i=1;i<=n;i++)
        {
            dp[i][0]=1;
            dp[i][1]=0;
            for(int j=1;j<i;j++)
            {
                if(a[j]<a[i])
                {
                    s[0]=dp[j][0]+1;
                    s[1]=dp[j][1]+1;
                    s[2]=dp[i][0];
                    s[3]=dp[i][1];
                    sort(s,s+4);
                    dp[i][0]=s[3];
                    dp[i][1]=s[2];
                }
            }
            ans[xx++]=dp[i][0];
            ans[xx++]=dp[i][1];
        }
        sort(ans,ans+2*n);
        printf("%d\n",ans[xx-2]);
    }
    return 0;
}


hdu 5087 Revenge of LIS II lcs变形

标签:acm   dp   lcs   

原文地址:http://blog.csdn.net/qq_16843991/article/details/40865639

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!