Revenge of LIS II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 530 Accepted Submission(s): 175
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.
---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
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 2HintFor 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.
题意:求第二场最长上升子序列。
做法:求出最长上升子序列,并记录,判断是否有多种上升序列,如果存在就输出最长上升子序列,否则就输出最长值-1.
#include <iostream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include<map>
#include <algorithm>
#include<ctime>
#define esp 1e-6
#define LL unsigned long long
#define inf 0x0f0f0f0f
using namespace std;
int main()
{
int i,j,n,a[10005],b[10005],ans,v[10005];
int t;
scanf("%d",&t);
while(t--)
{
memset(v,0,sizeof(v));
scanf("%d",&n);
for(i=0;i<n;i++)
cin>>a[i];
b[0]=1;
for(i=1;i<n;i++)
{
b[i]=1;
for(j=0;j<i;j++)
if(a[i]>a[j])
{
if(b[j]+1>b[i])
{
b[i]=b[j]+1;
v[i]=v[j];
}
else if(b[i]==b[j]+1)
v[i]=1;
}
}
int mm,flag,cnt;
mm=0;
for(i=0;i<n;i++)
mm=max(mm,b[i]);
cnt=flag=0;
for(i=0;i<n;i++)
{
if(b[i]==mm)
{
cnt++;
if(v[i]==1)
flag=1;
}
}
if(cnt>1||flag)
printf("%d\n",mm);
else
printf("%d\n",mm-1);
}
return 0;
}