hdu 5087(次长上升子序列)

Revenge of LIS II

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


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.
 

 

Source
 
题意:求解一个串中次长上升子序列的的长度。
题解:主要是求出有多少个最长上升子序列,如果个数大于1,那么次长就是最长,如果为1 ,那么次长就为最长的减一。关键是怎么统计最长上升子序列的个数??这里用到一个数组记录dp[i]的个数,当dp[j]+1>dp[i]时,dp[i]的个数为num[j],如果dp[j]+1==dp[i],那么 num[i] = num[i]+num[j]
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N = 1005;
int dp[N],num[N];
int a[N];
int main(){
    int tcase;
    scanf("%d",&tcase);
    while(tcase--){
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }
        int ans = dp[1] = num[1]= 1;
        for(int i=2;i<=n;i++){
            dp[i] =num[i]= 1;
            for(int j=1;j<i;j++){
                if(a[i]>a[j]){
                    if(dp[i]==dp[j]+1){
                        num[i]+=num[j];
                   }
                   if(dp[i]<dp[j]+1){
                        dp[i] = dp[j]+1;
                        num[i] = num[j];
                   }
                }
            }
            ans = max(ans,dp[i]);
        }
        int cnt=0;
        for(int i=1;i<=n;i++){
            if(dp[i]==ans){
                cnt+=num[i];
            }
        }
        if(cnt==1) printf("%d\n",ans-1);
        else printf("%d\n",ans);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/liyinggang/p/5658541.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值