Revenge of LIS II
Problem DescriptionIn 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.---WikipediaToday, 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.InputThe 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 <= 1002. 2 <= N <= 10003. 1 <= Ai <= 1 000 000 000OutputFor each test case, output the length of the second longest increasing subsequence.Sample Input3 2 1 1 4 1 2 3 4 5 1 1 2 2 2Sample Output1 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.题目大意:
求第二长的绝对递增子序列的长度。
解题思路:
错误思路:
求出用于求最长绝对递增子序列的dp数组,sort之后输出dp[N-1]。
未考虑到dp[N]可以有多种方式构成。eg:1 1 2 就应该输出2。
正确思路:
每次求dp[i]的时候,用c[i]记录有多少种情况来构成此最优解。
求出ans=max(dp[1],dp[2]...dp[N]).
在求出 sum=sum{ c[i] | dp[i]==ans }
若sum!=1 说明最优解有多种可能的构成方式。输出ans即可。
若sum==1 输出ans-1
Code:
1 /************************************************************************* 2 > File Name: BestCode#16_1002.cpp 3 > Author: Enumz 4 > Mail: 369372123@qq.com 5 > Created Time: 2014年11月01日 星期六 17时44分05秒 6 ************************************************************************/ 7 8 #include9 #include 10 #include 11 #include 12 #include 13 #include 14 #include
15 #include 16 #include