zoukankan      html  css  js  c++  java
  • HDU 5904 LCIS

    题目链接:

    http://acm.hdu.edu.cn/showproblem.php?pid=5904

    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 (1n,m100000) -- the length of two sequences. The second line contains n integers: a1,a2,...,an (1ai106). The third line contains n integers: b1,b2,...,bm (1bi106).

    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
     
     
    Hint:
     
    官方题解:
    fif(i)是以aiai​​结尾的最大值, gig(i)是以bibi​​结尾的最大值. 答案就是maxaibjminfigjmaxai​​=bj​​​​{min(f(i),g(j)}. ff和gg随便dp一下就出来了.
     
    代码:
    #include <cmath>
    #include <cstdio>
    #include <cstring>
    #include <algorithm> 
    using namespace std;
    const int maxn = 1e6+10;
    #define inf 0x3f3f3f3f
    #define met(a,b) memset(a,b,sizeof(a))
    int num1[maxn],num2[maxn];
    int dp1[maxn],dp2[maxn];
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            int n,m;
            met(dp1,0);met(dp2,0);
            scanf("%d%d",&n,&m);
            for(int i=0;i<n;i++)
            {
                scanf("%d",&num1[i]);
                dp1[num1[i]]=max(dp1[num1[i]],dp1[num1[i]-1]+1);
            }
            for(int i=0;i<m;i++)
            {
                scanf("%d",&num2[i]);
                dp2[num2[i]]=max(dp2[num2[i]],dp2[num2[i]-1]+1);
            }
            int ans=-inf;
            for(int i =0;i<n;i++)
                ans=max(ans,min(dp1[num1[i]],dp2[num1[i]]));
            printf("%d
    ",ans);
        }
    }
    
  • 相关阅读:
    oracle安装过程中遇到的问题
    HttpContext.Current 的缺陷
    oracle 复杂语句
    .net 中 ref out params的区别
    查看修改mysql编码方式[转载]
    Oracle 中的dual是什么东西
    KindEditor问题汇总【不定时更新】
    java中import、package作用和用法
    [笔记]: 哈弗曼树(最优二叉树) 标签: 二叉树 2017-05-17 16:38 34人阅读 评论(0) 收
    [noip 2004普及组] FBI树 标签: 二叉树递归 2017-05-17 14:29 40人阅读 评论(0)
  • 原文地址:https://www.cnblogs.com/TAT1122/p/5910444.html
Copyright © 2011-2022 走看看