Revenge of LIS IITime
limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1258 Accepted Submission (s): 423
Problem DescriptionIn Computer Science, the longest increasing subsequence problem are to find a subsequence of a given SEQ Uence in which the subsequence's elements be in sorted order, lowest to highest, and in which the subsequence is as long As possible. This subsequence are not necessarily contiguous, or unique.
---Wikipedia
Today, LIS takes revenge on you, again. You mission isn't calculating the length of longest increasing subsequence, but the length of the second longest Increasi Ng Subsequence.
Subsequence is different if and only they has different length, or has at least one different element index in the s Ame 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 is 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
Outputfor each test case, output the length of the second longest increasing subsequence.
Sample Input
321 141 2 3 451 1 2 2 2
Sample Output
Hintfor the first sequence, there is, and increasing subsequence: [1], [1]. The length of the second longest increasing subsequence is also 1, same with the length of LIS.
Sourcebestcoder Round #16
To find the length of the second-longest ascending subsequence in all ascending subsequence sequences of a sequence.
Solution idea: According to DP to solve a sequence of the maximum increment sub-sequence of the template, Dp[i] indicates the length of the longest ascending subsequence ending with number I, c[i] means the number of methods that reach dp[i], such as sequence 1 2 2 3 2;dp[2]=2,c[2]=1;dp[3]=2,c[3]= 1;dp[4]=3,c[4]=2.
The number of times the maximum increment subsequence appears, and if one occurs, the maximum length is reduced by one output, and if more than one time is present, the maximum length is output.
The code is as follows:
#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 1000000007using 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;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 5087 Revenge of LIS II (sub-large increment subsequence)