标签:des style blog http io color os ar java
DP的时候记录下是否可以从两个位置转移过来。。。。
3 2 1 1 4 1 2 3 4 5 1 1 2 2 2
1 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.
#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;
}
HDOJ 5087 Revenge of LIS II DP
标签:des style blog http io color os ar java
原文地址:http://blog.csdn.net/ck_boss/article/details/40691801