HDU 5087 Revenge of LIS II(次大递增子序列)

Revenge of LIS II

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

Total Submission(s): 1258    Accepted Submission(s): 423

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

BestCoder Round #16

题目大意:求一个序列的所有递增子序列中第二长的那个递增子序列的长度。

解题思路:根据dp求解一个序列的最大递增子序列的模板,dp[i]表示以第i个数结尾的最长上升子序列的长度 ,c[i]表示到达dp[i]的方法数,比如序列1 2 2 3 2;dp[2]=2,c[2]=1;dp[3]=2,c[3]=1;dp[4]=3,c[4]=2。

最后查询最大递增子序列出现的次数,如果出现一次,就最大长度减一输出;如果出现不止一次,就输出最大长度。

代码如下:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#include <limits.h>
#define debug "output for debug\n"
#define pi (acos(-1.0))
#define eps (1e-6)
#define inf (1<<28)
#define sqr(x) (x) * (x)
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
const int MAX=1005;
int a[MAX],c[MAX],dp[MAX];
int main()
{
    int i,j,n,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        int ans=0;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            dp[i]=1,c[i]=1;
            for(j=1;j<i;j++)
            {
                if(a[i]>a[j]&&dp[i]<dp[j]+1)
                {
                    dp[i]=dp[j]+1;
                    c[i]=c[j];
                }
                else if(dp[i]==dp[j]+1)
                    c[i]=2;
            }
            ans=max(ans,dp[i]);
        }
        int num=0;
        for(i=1;i<=n;i++)
        {
            if(dp[i]==ans)
                num+=c[i];
        }
        if(num==1)
            printf("%d\n",ans-1);
        else
            printf("%d\n",ans);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-11 02:38:53

HDU 5087 Revenge of LIS II(次大递增子序列)的相关文章

hdu 5087 Revenge of LIS II(LIS)

题目连接:hdu 5087 Revenge of LIS II 题目大意:给定一个序列,求第2长的LIS长度. 解题思路:用o(n^2)的算法求LIS,每个位置维护两个值,最大和最小即可.注意的是dp[0]中的最大第二大不能都复制成0. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1005; int N, A[maxn],

hdu 5087 Revenge of LIS II(BestCoder Round #16)

Revenge of LIS II                                                  Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 528    Accepted Submission(s): 173 Problem Description In computer science, th

HDU 5087 Revenge of LIS II(次长上升子序列)

题意  求一个序列的所有上升子序列中第二长的那个的长度 简单的dp   d[i]表示以第i个数结尾的最长上升子序列的长度  c[i]表示到达d[i]的方法数  如序列1 1 2  d[3]=2,c[3]=2  因为选1 3位置和 2 3位置的都可以得到d[3]=2 递推过程很简单 d[i]=max{d[j]+1}其中a[i]>a[j]&&i>j 最后看d[1~n]中最大的数出现了几次  出现了不止一次就直接输出否则就减一输出咯 #include <cstdio> #

hdu 5087 Revenge of LIS II 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 proble

hdu 5087 Revenge of LIS II ( LIS ,第二长子序列)

链接:hdu 5087 题意:求第二大的最长升序子序列 分析:这里的第二大指的是,全部的递增子序列的长度(包含相等的), 从大到小排序后.排在第二的长度 BestCoder Round #16 上的第二题,注意  1 1 2 这组数据,答案应为2 思路1.每次将最长的两个上升子序列长度记录.最后再排序,取第二大的就可以 思路2.假设最长的上升子序列长度(ans)唯一,那第二大应为ans-1 否则,第二大的就为 ans [cpp] view plaincopyprint? #include<std

hdu 5087 Revenge of LIS II

http://acm.hdu.edu.cn/showproblem.php?pid=5087 题意求第二长的上升序列. 在求最长上升序列的同时加上一个数组,来记录以i为结尾的有多少条序列.如果n+1为结尾有多条,就输出dp[n+1]-1; 否则在这个最长的序列上每一个节点是不是都是num[i]==1,如果是,就输出dp[n+1]-2;否则输出dp[n+1]-1: 1 #include <cstdio> 2 #include <cstring> 3 #include <algo

hdu 5087 Revenge of LIS II (DP)

题意: N个数,求第二长上升子序列的长度. 数据范围: 1. 1 <= T <= 1002. 2 <= N <= 10003. 1 <= Ai <= 1 000 000 000 思路: 数据给的很暧昧,用n^2的算法可以过.故用n^2算法.只要在DP过程中记录得到f[i]是否只有一种方法即可.详看代码. 代码: int T,n; int a[1005],f[1005]; bool NOTalone[1005]; int main(){ //freopen("t

hdoj 5087 Revenge of LIS II 【第二长单调递增子】

称号:hdoj 5087 Revenge of LIS II 题意:非常easy,给你一个序列,让你求第二长单调递增子序列. 分析:事实上非常easy.不知道比赛的时候为什么那么多了判掉了. 我们用O(n^2)的时间求单调递增子序列的时候,里面在加一层循环维护sum数组.表示前面有几个能够转移当当前,求前面sum的和保存到当前. 最后求最后一个sum[n-1]是否为1就ok.为1的话在最长的基础上减一,否则就是最长的. AC代码: #include <iostream> #include &l

hdoj 5087 Revenge of LIS II 【第二长单调递增子序列】

题目:hdoj 5087 Revenge of LIS II 题意:很简单,给你一个序列,让你求第二长单调递增子序列. 分析:其实很简单,不知道比赛的时候为什么那么多了判掉了. 我们用O(n^2)的时间求单调递增子序列的时候,里面在加一层循环维护sum数组,表示前面有几个可以转移当当前,求前面sum的和保存到当前. 最后求最后一个sum[n-1]是否为1就ok,为1的话在最长的基础上减一,否则就是最长的. AC代码: #include <iostream> #include <algor