mthoutai

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理


DP的时候记录下能否够从两个位置转移过来。

。。。

Revenge of LIS II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 393    Accepted Submission(s): 116


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.
 

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
 

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 2
Hint
For 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
 


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int n;
int a[1100],len[1100];
bool db[1100];

int main()
{
  int T_T;
  scanf("%d",&T_T);
  while(T_T--)
    {
      memset(len,0,sizeof(len));
      memset(db,false,sizeof(db));

      scanf("%d",&n);
      int LIS=-1;
      for(int i=0;i<n;i++)
        {
          scanf("%d",a+i);
          len[i]=1;
          int mxlen=-1,mxp=-1;
          for(int j=0;j<i;j++)
            {
              if(a[j]<a[i])
                {
                  if(len[j]+1>mxlen)
                    {
                      mxlen=len[j]+1; mxp=j;
                    }
                }
            }
          len[i]=max(len[i],mxlen);
          int c1=0;
          for(int j=0;j<i;j++)
            {
              if(a[j]>=a[i]) continue;
              if(len[j]+1==len[i])
                {
                  c1++;
                  if(db[j]==true)
                    {
                      db[i]=true;
                    }
                }
            }
          if(c1>=2)
            {
              db[i]=true;
            }
        }
      for(int i=0;i<n;i++)
        {
          if(LIS<len[i])
            {
              LIS=len[i];
            }
        }
      bool flag=false;
      int c1=0;
      for(int i=0;i<n&&flag==false;i++)
        {
          if(LIS==len[i])
            {
              if(db[i]==true) flag=true;
              c1++;
            }
        }
      if(c1>=2||flag)
        {
          printf("%d\n",LIS);
        }
      else printf("%d\n",max(1,LIS-1));
    }
  return 0;
}



posted on 2017-06-07 21:24  mthoutai  阅读(180)  评论(0编辑  收藏  举报