您的位置:首页 > 其它

Revenge of LIS II (hdu 5087 LIS)

2014-11-02 16:26 281 查看


Revenge of LIS II

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 720 Accepted Submission(s): 236



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
HintFor 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.




Source

BestCoder Round #16



Recommend

heyang | We have carefully selected several similar problems for you: 5089 5088 5085 5084 5081



题意:求第二长的绝对递增子序列的长度。

思路:每次求dp[i]的时候,用flag[i]记录有多少种情况来构成此最优解。求出ans=max(dp[1],dp[2]...dp
).再求出 sum,sum为最长上升子序列的个数,

若sum!=1 说明最优解有多种可能的构成方式。输出ans即可。若sum==1 输出ans-1。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1005
#define MAXN 2005
#define mod 1000000007
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std;

__int64 a[2000],dp[2000],flag[maxn];

__int64 LIS(__int64 a[],__int64 n)
{
    __int64 i,j,m=0,ans=0;;
    memset(dp,0,sizeof(dp));
    dp[1]=1;
    for(i=2;i<=n;i++)
    {
        m=0;
        for(j=1;j<i;j++)
        {
            if(a[i]>a[j])
            {
                if (dp[j]>m)
                {
                   m=dp[j];
                   flag[i]=flag[j];
                }
                else if (m==dp[j])
                    flag[i]=2;
            }
        }
        dp[i]=m+1;
        if (ans<dp[i])
            ans=dp[i];
    }
    int sum=0;
    for (i=1;i<=n;i++)
        if (dp[i]==ans)
            sum+=flag[i];
    if (sum==1)
        return ans-1;
    else
        return ans;
}

int main ()
{
    int cas;
    scanf("%d",&cas);
    while (cas--)
    {
        __int64 n,i;
        scanf("%I64d",&n);
        for(i=1;i<=n;i++)
        {
            scanf("%I64d",&a[i]);
            flag[i]=1;
        }
        printf("%I64d\n" ,LIS(a,n));
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: