HDU 5078 Revenge of lis ii (dp LIS)

Source: Internet
Author: User

HDU 5078 Revenge of lis ii (dp LIS)

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.
--- 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.
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 <= 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
132HintFor 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. 

SourceBestCoder Round #16
Recommend

Reference: http://blog.csdn.net/acvay/article/details/40686171


During the competition, I failed to read the question and started to get the result hack. Later I thought about the nlogn method, but it was fruitless. I should come up with it later and paste the method code later.


#include
 
  #include
  
   #include
   
    #include#include
    
     #include
     
      #include
      
       #include
       
        #define L(x) (x<<1)#define R(x) (x<<1|1)#define MID(x,y) ((x+y)>>1)#define eps 1e-8using namespace std;#define N 10005int a[N],dp[N],c[N];int n;int main(){int i,j,t;scanf("%d",&t);while(t--){scanf("%d",&n);for(i=1;i<=n;i++)scanf("%d",&a[i]); int ans=0; for(i=1;i<=n;i++){dp[i]=1;c[i]=1;for(j=1;j
        
         dp[i]) { dp[i]=dp[j]+1; c[i]=c[j]; } elseif(dp[j]+1==dp[i]) c[i]=2;}if(dp[i]>ans)ans=dp[i];}j=0;for(i=1;i<=n;i++)if(dp[i]==ans){j+=c[i];if(j>1)break;}if(j>1)printf("%d\n",ans);elseprintf("%d\n",ans-1);}return 0;}
        
       
      
     
    
   
  
 




Related Article

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.