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

找第二大的上升子序列。

开个数组再记录第二大的就行了。

官方题解:

LIS的O(N2)做法是dp[i]=max(dp[j]|a[j]<a[i])+1。如果需要第二大的,再额外DP一个至少有两个不同递增序列结束在i的最大长度。那么dpdirty[i]=max(dpdirty[j]|a[j]<a[i])+1,或者dpdirty[i]=max(dp[j]|Count[dp[j]]>1)+1。
这个思路不是很容易推广到O(NlogN)的LIS算法中,诸位可以思考一下。
代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1110;
int dp[maxn][2];
int a[maxn];
int ans[2*maxn];
int cur;
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0; i<n; i++)
            scanf("%d",&a[i]);
        cur=0;
        memset(dp,0,sizeof(dp));
        for(int i=0; i<n; i++)
        {
            dp[i][0] = 1;
            for(int j=0; j<i; j++)
                if(a[j]<a[i])
                {
                    int x = dp[j][0] + 1;
                    int y = dp[j][1] + 1;
                    if(x>dp[i][0])
                        swap(dp[i][0],x);
                    if(x > dp[i][1])
                        dp[i][1] = x;
                    if(y > dp[i][1])
                        dp[i][1] = y;
                }
            ans[cur++] = dp[i][0];
            ans[cur++] = dp[i][1];
        }
        sort(ans,ans+cur);
        printf("%d\n",ans[cur-2]);
    }
}

时间: 2024-08-08 05:38:12

hdu 5087 Revenge of LIS II(BestCoder Round #16)的相关文章

hdu 5088 Revenge of Nim II(BestCoder Round #16)

Revenge of Nim II                                                          Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 203    Accepted Submission(s): 63 Problem Description Nim is a mathema

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 f

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],

HDOJ 题目5087 Revenge of LIS II(第二长LIS)

Revenge of LIS II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1195    Accepted Submission(s): 400 Problem Description In computer science, the longest increasing subsequence problem is to f

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(次长上升子序列)

题意  求一个序列的所有上升子序列中第二长的那个的长度 简单的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 5078 Revenge of LIS II(dp LIS)

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 po

HDU-5086-Revenge of Segment Tree (BestCoder Round #16)

Revenge of Segment Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 205    Accepted Submission(s): 83 Problem Description In computer science, a segment tree is a tree data structure for st