hdu 5087 Revenge of LIS II 题解(DP) BestCoder Round #16 1002

来源:互联网 发布:标书软件 编辑:程序博客网 时间:2024/06/01 19:10

Revenge of LIS II(BestCoder Round #16  :1002

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


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
321 141 2 3 451 1 2 2 2
 


Sample Output
132
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.
 题意:给你一个序列,问你这个序列的第二子最长递增序列的长度是多少。

思路一: 用dp方法求最长子序列,加一个数组b[i]来表示dp[i]时有几种情况。用一个变量pd表示有几个不同位置结束且长度为max的子序列。
            用变量k来记录最大序列结束的位置,只要满足b[k]>1或pd>1就输出max,否则输出max-1;

这题调试了很久,主要是错在对数组b的操作上。

AC代码:

include<iostream>#include<algorithm>#include<cstring>#include<cstdio>using namespace std;long long dp[2000],a[2000],b[2000];int main(){    long long n,t,i,j,pd;    long long ma,k;    cin>>t;    while(t--){        memset(dp,0,sizeof(dp));        memset(b,0,sizeof(b));            cin>>n;        for(i=1;i<=n;i++){            cin>>a[i];        }        b[1]=1;dp[1]=1;        for(i=1;i<=n;i++){                dp[i]=1;                b[i]=1;            for(j=1;j<i;j++){                if(a[i]>a[j]){                    if(dp[i]<dp[j]+1){                        dp[i]=dp[j]+1;                        b[i]=b[j];                    }                    else if(dp[i]==dp[j]+1)                        b[i]+=b[j];                }            }        }        ma=0;        for(i=1;i<=n;i++){            if(dp[i]>ma){                ma=dp[i];                k=i;            }        }        pd=0;        for(i=1;i<=n;i++){            if(dp[i]==ma)                pd++;        }        if(b[k]>1||pd>1)            printf("%I64d\n",ma);        else            printf("%I64d\n",ma-1);    }return 0;}



思路二:

分析:其实很简单,不知道比赛的时候为什么那么多了判掉了。

我们用O(n^2)的时间求单调递增子序列的时候,里面在加一层循环维护sum数组,表示前面有几个可以转移当当前,求前面sum的和保存到当前。

最后求最后一个sum【n-1】是否为1就ok,为1的话在最长的基础上减一,否则就是最长的。


借鉴:http://blog.csdn.net/y990041769/article/details/40735683



AC代码:


#include <iostream>#include <algorithm>#include <string>#include <math.h>#include <vector>#include <cstring>#include <cstdio>using namespace std;const long long N =  1100;const long long Mod = 1000000007;typedef long long LL;int a[N],dp[N],sum[N];int main(){    int T;    scanf("%d",&T);    while(T--)    {        int n;        scanf("%d",&n);        int ma = 0;        for(int i=0;i<n;i++){            scanf("%d",&a[i]);            ma = max(a[i],ma);        }        a[n++] = ma+1;        memset(sum,0,sizeof(sum));        dp[0] = 1;sum[0] = 1;        for(int i=1;i<n;i++)        {            int tmp = 0;            for(int j=i-1;j>=0;j--)            {                if(a[i]>a[j] && dp[j]>tmp)                    tmp = dp[j];            }            for(int j=i-1;j>=0;j--)            {                if(dp[j]==tmp && a[j]<a[i])                    sum[i]+=sum[j];            }            if(sum[i]==0)                sum[i] = 1;            dp[i] = tmp + 1;        }        int ans = 0;        for(int i=0;i<n;i++)            ans = max(ans,dp[i]);        if(sum[n-1]==1)            ans--;        printf("%d\n",ans-1);    }    return 0;}



0 0
原创粉丝点击