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. golang passing an array to a function

    package main import “fmt” func fp(a *[]int) { fmt.Println(a) } func main() { ; i < ; i++ { fp(&am ...

  2. meld文件的脚本

    今天模仿着别人的脚本,结合网上的资料,摸索着写了一个简单的脚本,用来打开meld 工具.这个脚本虽然简单,但这是第一次自己写脚本,记录下来,作为自己python学习的起点.代码如下 #/use/bin ...

  3. 学习笔记(二):javascript之dom操作

    一.document.getElementById()    根据Id获取元素节点 <div id="div1"> <p id="p1"> ...

  4. Flume Channel Selectors官网剖析(博主推荐)

    不多说,直接上干货! Flume Sources官网剖析(博主推荐) Flume Channels官网剖析(博主推荐) 一切来源于flume官网 http://flume.apache.org/Flu ...

  5. jquery中prop()和attr()的使用

    jquery1.6+出现的prop()方法. • 对于HTML元素本身就带有的固有属性,在处理时,使用prop方法. • 对于HTML元素我们自己自定义的DOM属性,在处理时,使用attr方法. • ...

  6. 使用javascript实现图片上下切换效果并且实现顺序循环播放

    <!doctype html><html lang="en"><head> <meta charset="UTF-8" ...

  7. 洛谷—— P1091 合唱队形

    https://www.luogu.org/problem/show?pid=1091#sub  ||  http://codevs.cn/problem/1058/ 题目描述 N位同学站成一排,音乐 ...

  8. 全双工 串口 stm32

  9. 9.5 Binder系统_驱动情景分析_transaction_stack机制

    参考文章:http://www.cnblogs.com/samchen2009/p/3316001.html test_server服务进程可能有多个线程,而在发送数据的时候handle只表示了那个进 ...

  10. FTP、SSH、NFS等环境工具的安装

    注意:通过ftp互传文件或者通过ssh登录的时候,ubuntu需要使用bridged上网环境 ftp: sudo apt-get install vsftpd sudo vi /etc/vsftpd. ...