Revenge of LIS II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 310 Accepted Submission(s): 87
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.
Source
在dp的时候再建立一个数组,代表这个点为终点的最长上升子序列是否有多种选择。然后保留一个数的前一个选择,这样的话,做完之后check一下是否中间某个点有多种选择就可以了。
/***********************************************\
|Author: YMC
|Created Time: 2014/11/1 19:19:18
|File Name: b.cpp
|Description:
\***********************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define mset(l,n) memset(l,n,sizeof(l))
#define rep(i,n) for(int i=0;i<n;++i)
#define maxx(a) memset(a, 0x3f, sizeof(a))
#define zero(a) memset(a, 0, sizeof(a))
#define srep(i,n) for(int i = 1;i <= n;i ++)
#define MP make_pair
const int inf=0x3f3f3f3f ;
const double eps=1e-8 ;
const double pi=acos (-1.0);
typedef long long ll;
using namespace std;
int da[1005];
int dp[1005];
bool fg[1005];
int pre[1005];
int n;
void dodp() {
memset(fg,false,sizeof(fg)); //表示没有重复
for(int i=1;i<=n;++i) {
dp[i] = 1;
pre[i] = i;
}
for(int i=1;i<=n;++i) {
int tp = -1,p = -1;
for(int j=i-1;j>=1;--j) {
if(da[j] < da[i] && dp[j] > tp) {
tp = dp[j]; p = j;
fg[i] = false;
} else if(da[j] < da[i] && dp[j] == tp){
fg[i] = true;
}
}
if(p == -1) {
fg[i] = false;
} else {
dp[i] = max(dp[i],tp + 1);
pre[i] = p;
}
}
int ans = 0;
int pp = -1;
bool ff = false;
for(int i=1;i<=n;++i) {
if(dp[i] > ans) {
ans = dp[i];
pp = i;
ff = false;
} else if(dp[i] == ans){
ff = true;
}
}
if(ff){
printf("%d\n",ans);
return ;
}
if(ans == 1) {
puts("1");
return ;
}
bool flag = true;
while(dp[pp] != 1) {
if(fg[pp]) {
flag = false;
break;
} else {
pp = pre[pp];
}
}
if(!flag) {
printf("%d\n",ans);
} else {
printf("%d\n",ans-1);
}
}
int main() {
//freopen("input.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
srep(i,n) scanf("%d",&da[i]);
dodp();
}
return 0;
}