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. ajax中的POST和GET传值

    ajax中的POST和GET传值 转自:http://www.cnblogs.com/jtome/archive/2008/12/04/1347864.html Ajax中我们经常用到get和post ...

  2. 5. Spring Boot 拦截器

    转自:https://blog.csdn.net/catoop/article/details/50501696

  3. setAttribute的浏览器兼容性

    1.element要用getElementById 或者是ByTagName来得到 2.setAttribute("class", vName)中class是指改变"cl ...

  4. jvm compile

    >>>Making sec-files-win @ Thu Oct 17 20:34:02 CST 2013 ... >>>Making jgss-files @ ...

  5. [D3] Reuse Transitions in D3 v4

    D3 transitions start executing as soon as they’re created, and they’re destroyed once they end. This ...

  6. JDBC 专题

    digest: getFetchSize()方法不是获得记录数,而是获得每次抓取的记录数,默认是0,也就是说不限制.可以用setFetchSize()来设置,而getFetchSize()是用来读出那 ...

  7. POJ 1325 Machine Schedule(zoj 1364) 最小覆盖数

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=364 http://poj.org/problem?id=1325 题目大意: ...

  8. 洛谷——P2241 统计方形(数据加强版)

    https://www.luogu.org/problem/show?pid=2241 题目背景 1997年普及组第一题 题目描述 有一个n*m方格的棋盘,求其方格包含多少正方形.长方形 输入输出格式 ...

  9. 仿招商银行载入loading效果

    在招商银行android手机app中.有例如以下图所看到的的loading载入效果: 实现这个效果还是比較简单,就是自己定义dialog,设置自己想要的布局.然后设置旋转动画. 主要步骤: 1,写布局 ...

  10. Android android.database.CursorIndexOutOfBoundsException:Index -1 requested, with a size of 1

    Android中数据库处理使用cursor时,游标不是放在为0的下标,而是放在为-1的下标处开始的. 也就是说返回给cursor查询结果时,不能够马上从cursor中提取值. 下面的代码会返回错误 U ...