hdu-5904 LCIS(水题)

题目链接:

LCIS

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 458    Accepted Submission(s): 212

Problem Description

Alex has two sequences a1,a2,...,an and b1,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 T, indicating the number of test cases. For each test case:

The first line contains two integers n and m (1≤n,m≤100000) -- the length of two sequences. The second line contains n integers: a1,a2,...,an (1≤ai≤106). The third line contains n integers: b1,b2,...,bm (1≤bi≤106).

There are at most 1000 test cases and the sum of n and m does not exceed 2×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

1

Sample Output

1

5

0

题意:

求这两个序列的最长公共子序列且是一段连续的值得最长长度;

思路:

两个序列A,B,fa[i]表示A序列中以i为结尾的最长的子序列的最长长度,fb[i]也是一样,然后取max(min(fa[i],fb[i]));

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map>

using namespace std;

#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
#define lson o<<1
#define rson o<<1|1
typedef  long long LL;

template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<‘0‘||CH>‘9‘;F= CH==‘-‘,CH=getchar());
    for(num=0;CH>=‘0‘&&CH<=‘9‘;num=num*10+CH-‘0‘,CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + ‘0‘);
    putchar(‘\n‘);
}

const LL mod=1e9+7;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=1e6+10;
const int maxn=1e5+10;
const double eps=1e-12;

int n,m,a[maxn],b[maxn],fa[N],fb[N];

int main()
{
    int t;
    read(t);
    while(t--)
    {
        read(n);read(m);
        For(i,1,n)read(a[i]);
        For(i,1,m)read(b[i]);
        For(i,1,n)fa[a[i]-1]=fa[a[i]]=0,fb[a[i]-1]=fb[a[i]]=0;
        For(i,1,m)fa[b[i]-1]=fa[b[i]]=0,fb[b[i]-1]=fb[b[i]]=0;
        For(i,1,n)fa[a[i]]=fa[a[i]-1]+1;
        For(i,1,m)fb[b[i]]=fb[b[i]-1]+1;
        int ans=0;
        for(int i=1;i<=n;i++)ans=max(ans,min(fa[a[i]],fb[a[i]]));
        printf("%d\n",ans);
    }
    return 0;
}

  

时间: 2024-10-31 17:18:33

hdu-5904 LCIS(水题)的相关文章

HDU 2092 整数解 --- 水题

x+y = n, x*y = m; y = n - x; x * ( n - x) = m nx - x^2 = m; x^2 - nx + m = 0; △ = sqrt(n^2 - 4m) 要有整数解即△需要为可开方数即可. /* HDU 2092 整数解 --- 水题 */ #include <cstdio> #include <cmath> int main() { double n, m; while (scanf("%lf%lf", &n,

HDU 5904 - LCIS (BestCoder Round #87)

HDU 5904 - LCIS [ DP ]    BestCoder Round #87 题意: 给定两个序列,求它们的最长公共递增子序列的长度, 并且这个子序列的值是连续的 分析: 状态转移方程式: dp[a[i]] = max(dp[a[i]], dp[a[i]-1] + 1); 发现其实可以简化为 dp[a[i]] = dp[a[i]-1] + 1:因为计算过程中dp[a[i]]不会降低 对两个序列都求一遍,然后取两者最小值的最大值 1 #include <cstdio> 2 #inc

Girls&#39; research HDU - 3294(马拉车水题)

题意: 求最长回文串 长度要大于等于2  且输出起点和终点  输出回文串字符 这个字符还是要以给出的字符为起点a 输出 解析: 分析一下s_new串就好了 #include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <set> #include <vector> #include &l

hdu 5210 delete 水题

Delete Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5210 Description wld有n个数(a1,a2,...,an),他希望进行k次删除一个数的操作,使得最后剩下的n−k个数中有最多的不同的数,保证1≤n≤100,0≤k<n,1≤ai≤n(对于任意1≤i≤n) Input 多组数据(最多100组)对于每组数据:第一行:一个数n表示数的个数接下来一行:

HDU 5703 Desert 水题 找规律

已知有n个单位的水,问有几种方式把这些水喝完,每天至少喝1个单位的水,而且每天喝的水的单位为整数.看上去挺复杂要跑循环,但其实上,列举几种情况之后就会发现是找规律的题了= =都是2的n-1次方,而且这题输出二进制数就行了......那就更简单了,直接输出1,然后后面跟n-1个0就行了╮(╯_╰)╭ 下面AC代码 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm>

hdu 4802 GPA 水题

GPA Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4802 Description In college, a student may take several courses. for each course i, he earns a certain credit (ci), and a mark ranging from A to F, which is c

hdu 1878 欧拉回路 水题。测试数据貌似有点问题

欧拉回路 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10135    Accepted Submission(s): 3708 Problem Description 欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路.现给定一个图,问是否存在欧拉回路? Input 测试输入包含若干测试用例.每个测试用例

hdu 1015 Safecracker 水题一枚

题目链接:HDU - 1015 === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Klein safe behind a painting in the second-floor library. Klein safes are extremely rare; most of them, along with Klein and his factory, were destroyed in World

HDU 5904 LCIS (DP)

题意:给定两个序列,让你找出这两个序列的LCIS的长度. 析:DP a[i] 表示以ai结尾的最大值,b[i]表示以bi结尾的最大值. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostre

HDU 5211 Mutiple 水题

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5211 题解: 1.筛法: 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<algorithm> 5 #include<vector> 6 using namespace std; 7 8 const int maxn=10000+10; 9 const i