Hdoj topic 5087 Revenge of Lis II (second longest LIS)

Source: Internet
Author: User

Revenge of LIS IITime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1195 Accepted Submission (s): 400


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
Recommendheyang | We have carefully selected several similar problems for you:5213 5212 5211 5210 5209


This problem is too pit, the idea completely lead to find out Lis, and then determine whether the LIS only up
Otherwise, such as 1 1 2,lis = = 2, but the light so can not judge the length of the second large how much

Online is to record the number of LIS each location, if the last Lis only one output LIS-1, or to determine the LIS on each location on the LIS is unique, not the only output LIS, otherwise output LIS-1

AC Code

#include <stdio.h> #include <string.h> #define INF 0xfffffffint dp[1010],num[1010];int a[1010];int main () { int t;scanf ("%d", &t), while (t--) {int n,i,j;scanf ("%d", &n), and for (i=1;i<=n;i++) {scanf ("%d", &a[i]);DP [i ]=1;num[i]=1;} Num[n+1]=1;dp[n+1]=1;a[n+1]=inf;for (i=1;i<=n+1;i++) {for (j=1;j<i;j++) {if (a[i]>a[j]&&dp[i]< dp[j]+1) {dp[i]=dp[j]+1;num[i]=1;} Else{if (a[i]>a[j]&&dp[i]==dp[j]+1) {num[i]++;}}} if (num[n+1]>1) {printf ("%d\n", dp[n+1]-1); continue;} int K=n+1;while (k>0&&num[k]==1) {for (i=k-1;i>=1;i--) {if (Dp[k]==dp[i]+1&&a[k]>a[i]) { Break;}} K=i;} if (k==0) {printf ("%d\n", dp[n+1]-2);} elseprintf ("%d\n", Dp[n+1]-1);}}


Hdoj title 5087 Revenge of Lis II (second longest lis)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.