Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 519    Accepted Submission(s): 238

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
 
Source

【题解】

设q[i],表示以数字i为上升序列的最后一个元素的最长长度。

则if (q[i-1] == 0)

q[i-1] = 1;

else

q[i] = max(q[i-1]+1,q[i]);

最后枚举要枚举n+m个而不是枚举1..100W,不然会超时。

【代码】

#include <cstdio>
#include <algorithm>
#include <cstring> using namespace std; const int MAXN = 102001;
const int MAX_NUM = 1009999; int n,m,a[MAXN],b[MAXN],q1[MAX_NUM],q2[MAX_NUM]; void input_data()
{
scanf("%d%d", &n,&m);
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
if (q1[a[i]-1] == 0)
q1[a[i]] = 1;
else
if (q1[a[i]] < q1[a[i]-1]+1)
q1[a[i]] = q1[a[i] - 1] + 1;
}
for (int i = 1; i <= m; i++)
{
scanf("%d", &b[i]);
if (q2[b[i] - 1] == 0)
q2[b[i]] = 1;
else
if (q2[b[i]] < q2[b[i] - 1] + 1)
q2[b[i]] = q2[b[i] - 1] + 1;
}
} void get_ans()
{
int len = 0;
for (int i = 1; i <= n; i++)//枚举的是出现过的每个数字
{
int d = min(q1[a[i]], q2[a[i]]);
if (d > len)
len = d;
}
for (int i = 1; i <= m; i++)
{
int d = min(q1[b[i]], q2[b[i]]);
if (d > len)
len = d;
}
printf("%d\n", len);
} void init()
{
for (int i = 0; i <= 1000000; i++)
q1[i] = 0;
for (int i = 0; i <= 1000000; i++)
q2[i] = 0;
} int main()
{
// freopen("F:\\rush.txt", "r", stdin);
int t;
scanf("%d", &t);
while (t--)
{
init();
input_data();
get_ans();
}
return 0;
}

【14.06%】【hdu 5904】LCIS的更多相关文章

  1. 【改革春风吹满地 HDU - 2036 】【计算几何-----利用叉积计算多边形的面积】

    利用叉积计算多边形的面积 我们都知道计算三角形的面积时可以用两个邻边对应向量积(叉积)的绝对值的一半表示,那么同样,对于多边形,我们可以以多边形上的一个点为源点,作过该点并且过多边形其他点中的某一个的 ...

  2. 【黑金教程笔记之007】【建模篇】【Lab 06 SOS信号之二】—笔记

    控制模块的协调角色. 实验六用到了实验四的按键消抖模块debounce_module.v和实验五的sos_module.v. 设计思路: debounce_module.v看成一个输入,sos_mod ...

  3. -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】

    [把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...

  4. 七夕节 (HDU - 1215) 【简单数论】【找因数】

    七夕节 (HDU - 1215) [简单数论][找因数] 标签: 入门讲座题解 数论 题目描述 七夕节那天,月老来到数字王国,他在城门上贴了一张告示,并且和数字王国的人们说:"你们想知道你们 ...

  5. 【HDU 2255】奔小康赚大钱 (最佳二分匹配KM算法)

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  6. 【二分】【最长上升子序列】HDU 5489 Removed Interval (2015 ACM/ICPC Asia Regional Hefei Online)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5489 题目大意: 一个N(N<=100000)个数的序列,要从中去掉相邻的L个数(去掉整个区间 ...

  7. 【贪心】【模拟】HDU 5491 The Next (2015 ACM/ICPC Asia Regional Hefei Online)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5491 题目大意: 一个数D(0<=D<231),求比D大的第一个满足:二进制下1个个数在 ...

  8. 【动态规划】【二分】【最长上升子序列】HDU 5773 The All-purpose Zero

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5773 题目大意: T组数据,n个数(n<=100000),求最长上升子序列长度(0可以替代任何 ...

  9. 【动态规划】【KMP】HDU 5763 Another Meaning

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...

随机推荐

  1. vue踩坑记-在项目中安装依赖模块npm install报错

    在维护别人的项目的时候,在项目文件夹中安装npm install模块的时候,报错如下: npm ERR! path D:\ShopApp\node_modules\fsevents\node_modu ...

  2. java 并发原子性与易变性 来自thinking in java4 21.3.3

    java 并发原子性与易变性  具体介绍请參阅thinking in java4 21.3.3 thinking in java 4免费下载:http://download.csdn.net/deta ...

  3. 不重新启动VMWare虚拟机加入虚拟磁盘的方法(上)

    近期因为业务须要在不重新启动系统的前提下对系统进行扩容,前提是该系统做过lvm.可是没有足够的物理卷(硬盘),所以引出了改文.本文共分为上下两部分.这是第一部分. 文件夹 加入磁盘 做LVM 加入硬盘 ...

  4. ImageButton-设置background跟src

    xml中添加ImageButton的background跟src <ImageButton android:id="@+id/tv3" android:layout_widt ...

  5. go package的理解

    golang package是基本的管理单元, 同一个package下面,可以有非常多的不同文件,只要 每个文件的头部    都有 如 "package xxx" 的相同name, ...

  6. 二叉树的递归插入【Java实现】

    C++中由于有指针的存在,可以让二叉树节点指针的指针作为插入函数的实参,在函数体内通过*操作实现对真实节点指针.节点左孩子指针.节点右孩子指针的改变,这样很容易使用递归将大树问题转化到小树问题.但在J ...

  7. 【例 7-12 UVA - 1343】The Rotation Game

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 迭代加深搜索. 每次抽动操作最多只会让中间那一块的区域离目标的"距离"减少1. 以这个作为剪枝. 枚举最大深度. ...

  8. Android 一个异步SocketHelper

    发送流程:首先定义一个缓冲池,发送数据时仅仅是将待发送的数据加入到缓冲池中,再由后台的工作线程从缓冲池中取得待发送数据进行发送.可能某些情况下在数据发送完成时需要做一些处理(比如写日志),便定义了一个 ...

  9. [React Intl] Use a react-intl Higher Order Component to format messages

    In some cases, you might need to pass a string from your intl messages.js file as a prop to a compon ...

  10. swift 利用 Reflect(字典转模型)

    1.  导入Reflect(字典转模型)框架 2. 让它继承Reflect这个类,如下代码所示: class IWUser: Reflect { /** *  用户的ID */ var idstr:N ...