LCIS HDU - 5904
Alex has two sequences a1,a2,...,ana1,a2,...,an and b1,b2,...,bmb1,b2,...,bm. He wants find a longest common subsequence that consists of consecutive values in increasing order.
Input
There are multiple test cases. The first line of input contains an integer TT, indicating the number of test cases. For each test case:
The first line contains two integers nn and mm (1≤n,m≤100000)(1≤n,m≤100000) -- the length of two sequences. The second line contains nn integers: a1,a2,...,ana1,a2,...,an (1≤ai≤106)(1≤ai≤106). The third line contains nn integers: b1,b2,...,bmb1,b2,...,bm (1≤bi≤106)(1≤bi≤106).
There are at most 10001000 test cases and the sum of nn and mm does not exceed 2×1062×106.Output
For each test case, output the length of longest common subsequence that consists of consecutive values in increasing order.
Sample Input
3 3 3 1 2 3 3 2 1 10 5 1 23 2 32 4 3 4 5 6 1 1 2 3 4 5 1 1 2 1Sample Output
1 5 0
题意:给定两个序列,求它们的最长公共递增子序列的长度, 并且这个子序列的值是连续的。
分析:我们可以先处理a,b的每个数的最长连续长度,最终取两者最小值的最大值,状态转移方程:dp[a[i]] = max(dp[a[i]], dp[a[i]-1] + 1);
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn=1e6+7;
int dp1[maxn],dp2[maxn];
int a1[maxn],a2[maxn];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(dp1,0,sizeof(dp1));
memset(dp2,0,sizeof(dp2));
int n,m;
scanf("%d%d",&n,&m);
for(int i=0; i<n; i++)
scanf("%d",&a1[i]);
for(int i=0; i<m; i++)
scanf("%d",&a2[i]);
int maxx=0;
for(int i=0; i<n; i++)
{
dp1[a1[i]]=max(dp1[a1[i]],dp1[a1[i]-1]+1);
maxx=max(a1[i],maxx);
}
for(int i=0; i<m; i++)
{
dp2[a2[i]]=max(dp2[a2[i]],dp2[a2[i]-1]+1);
maxx=max(a2[i],maxx);
}
int ans=-1;
for(int i=1; i<=maxx; i++)
{
ans=max(ans,min(dp1[i],dp2[i]));
}
printf("%d\n",ans);
}
return 0;
}