【DP】 HDU 5087 Revenge of LIS II
求第二大的LIS
每种情况都要考虑到。。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <cmath>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#define cler(arr, val) memset(arr, val, sizeof(arr))
typedef long long LL;
const int MAXN = 102000;
const int MAXM = 6000010;
const int INF = 0x3f3f3f3f;
const int mod = 1000000007;
LL num[1400],dp[2][1555];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
LL n,t;
cin>>t;
while(t--)
{
cin>>n;
memset(dp,0,sizeof(dp));
memset(num,0,sizeof(num));
for(int i=1; i<=n; i++)
{
scanf("%I64d",&num[i]);
dp[0][i]=1;
}
LL maxx=1,ans=0;
for(int i=2; i<=n; i++)
{
for(int j=1; j<i; j++)
{
if(num[i]>num[j])
{
if(dp[0][i]<dp[0][j]+1){
dp[1][i]=dp[0][i];
dp[0][i]=dp[0][j]+1;
}
else if(dp[1][i]<dp[0][j]+1)
dp[1][i]=dp[0][j]+1;
if (dp[0][i]<dp[1][j]+1){
dp[1][i]=dp[0][i];
dp[0][i]=dp[1][j]+1;
}
else if (dp[1][i]<dp[1][j]+1)
dp[1][i]=dp[1][j]+1;
}
}
if(dp[0][i]==maxx)
ans=max(dp[0][i],ans);
else
{
ans=max(dp[1][i],ans);
maxx=max(maxx,dp[0][i]);
}
}
cout<<ans<<endl;
}
return 0;
}

浙公网安备 33010602011771号