Greatest Common Increasing Subsequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5444    Accepted Submission(s): 1755

Problem Description
This is a problem from ZOJ 2432.To make it easyer,you just need output the length of the subsequence.
 
Input
Each
sequence is described with M - its length (1 <= M <= 500) and M
integer numbers Ai (-2^31 <= Ai < 2^31) - the sequence itself.
 
Output
output print L - the length of the greatest common increasing subsequence of both sequences.
 
Sample Input
1

5
1 4 2 5 -12
4
-12 1 2 4

 
Sample Output
2
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  1422 1400 1418 1424 1401 

四种方法代码:

每一种都按照我觉得最容易理解的思路和最精简的写法写了,有些地方i 或者 i-1都可以,不必纠结。

#include<algorithm>
#include<string>
#include<string.h>
#include<stdio.h>
#include<iostream>
using namespace std;
#define N 505 int a[N],l1,l2,b[N],ma,f[N][N],d[N];
void solve1()//O(n^4)
{
ma = ;
memset(f, , sizeof(f) );
for(int i = ; i <= l1; i++)
for(int j = ; j <= l2; j++)
{
if(a[i-] == b[j-])
{
int ma1 = ;
for(int i1 = ; i1 < i; i1++)
for(int j1 = ; j1 < j; j1++)
if(a[i1-] == b[j1-] && a[i1-] < a[i-] && f[i1][j1] > ma1)//实际上a[i-1]==b[j1-1]可以省,因为既然f[i1][j1]+1>f[i][j]了,
ma1 = f[i1][j1]; /*说明肯定a[i-1]==b[j1-1]了,因为这种解法只有当a[i-1]==b[j-1]时,f[i][j]才不等于0*/
f[i][j] = ma1 + ;
}
ma = max(ma, f[i][j]);
}
cout<<ma<<endl;
}
void solve2()//O(n^3)
{
ma = ;
memset(f, , sizeof(f) );
for(int i = ; i <= l1; i++)
for(int j = ; j <= l2; j++)
{
f[i][j] = f[i-][j];
if(a[i-] == b[j-])
{
int ma1 = ;
for(int j1 = ; j1 < j; j1++)
{
if(b[j1-] < b[j-] && f[i-][j1] > ma1)
ma1 = f[i-][j1];
}
f[i][j] = ma1 + ;
}
ma = max(ma, f[i][j]);
}
cout<<ma<<endl;
}
void solve3()//O(n^2)
{
memset(f, , sizeof(f) );
for(int i = ; i <= l1; i++)
{
int ma1 = ;
for(int j = ; j <= l2; j++)
{
f[i][j] = f[i-][j];//带这种的一般都能压缩一维空间,也就是简化空间复杂度
if(a[i-] > b[j-] && f[i-][j] > ma1)
ma1 = f[i-][j];
if(a[i-] == b[j-])
f[i][j] = ma1 + ;
}
}
ma = -;
for(int j = ;j <= l2; j++)
ma=max(ma,f[l1][j]); cout<<ma<<endl;
}
void solve4()//O(n^2)//优化空间复杂度
{
ma = ;
memset(d, , sizeof(d) );
for(int i = ; i <= l1; i++)
{
int ma1 = ;
for(int j = ; j <= l2; j++)
{
if(a[i-] > b[j-] && d[j] > ma1)
ma1 = d[j];
if(a[i-] == b[j-])
d[j] = ma1 + ;
}
}
ma = -;
for(int j = ;j <= l2; j++)
ma = max(ma, d[j]); cout<<ma<<endl;
} int main()
{
int T;cin>>T;
while(T--)
{
scanf("%d", &l1);
for(int i = ; i < l1; i++)
scanf("%d", &a[i]);
scanf("%d", &l2);
for(int i = ; i < l2; i++)
scanf("%d", &b[i]);
// solve1();
// solve2();
// solve3();
solve4(); if(T)
printf("\n");
} return ;
}
/*
99 5
1 4 2 5 -12
4
-12 1 2 4 9
3 5 1 6 7 9 1 5 13
6
4 6 13 9 13 5
*/

HDU 1423 Greatest Common Increasing Subsequence(LICS入门,只要求出最长数)的更多相关文章

  1. HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)

    HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS) http://acm.hdu.edu.cn/showproblem.php?pi ...

  2. HDU 1423 Greatest Common Increasing Subsequence LCIS

    题目链接: 题目 Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...

  3. HDU 1423 Greatest Common Increasing Subsequence(LCIS)

    Greatest Common Increasing Subsequenc Problem Description This is a problem from ZOJ 2432.To make it ...

  4. HDU 1423 Greatest Common Increasing Subsequence

    最长公共上升子序列   LCIS 看这个博客  http://www.cnblogs.com/nuoyan2010/archive/2012/10/17/2728289.html #include&l ...

  5. HDU 1423 Greatest Common Increasing Subsequence ——动态规划

    好久以前的坑了. 最长公共上升子序列. 没什么好说的,自己太菜了 #include <map> #include <cmath> #include <queue> ...

  6. HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】

    HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...

  7. HDOJ 1423 Greatest Common Increasing Subsequence -- 动态规划

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1423 Problem Description This is a problem from ZOJ 2 ...

  8. POJ 1423 Greatest Common Increasing Subsequence【裸LCIS】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1423 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  9. HDU1423:Greatest Common Increasing Subsequence(LICS)

    Problem Description This is a problem from ZOJ 2432.To make it easyer,you just need output the lengt ...

随机推荐

  1. 误删除innodb ibdata数据文件-之恢复

    今天在群里看到有人说不熟悉innodb把ibdata(数据文件)和ib_logfile(事务日志)文件误删除了.不知道怎么解决.当时我也不知道怎么办.后来查阅相关资料.终找到解决方法.其实恢复也挺简单 ...

  2. HDU 4283 区间DP You Are the One

    题解 我使用记忆化搜索写的.

  3. STM32F407 ADC 个人笔记

    1. ADC概述(STM32F4xx系列) 3 个 ADC 可分别独立使用 也可使用双重/三重模式(提高采样率) 2 个通道组 规则通道:相当于正常运行的程序 注入通道:相当于中断(可以打断规则通道的 ...

  4. c标准库 徐明远 背景基础

    背景基础 1.c语言库用c语言编写   其他语言则不同 早期语言的库是用汇编语言编写的    不同的计算机体系结构有不同的汇编语言   所以在移植性方面差一点   而c语言可以编写出高度可移植性的代码 ...

  5. BZOJ 4822 [Cqoi2017]老C的任务 ——树状数组

    直接离散化之后用树状数组扫一遍. 把每一个询问拆成四个就可以做了. %Silvernebula 怒写KD-Tree #include <map> #include <cmath> ...

  6. [BZOJ1574] [Usaco2009 Jan]地震损坏Damage(贪心 + dfs)

    传送门 告诉你一些点不能到达1,由于是双向边,也就是1不能到达那些点 那么从1开始dfs,如果当前点能到达不能到达的点,那么当前点就是损坏的. #include <cstdio> #inc ...

  7. 算法复习——2—sat(bzoj2199)

    题目: Description 由于对Farmer John的领导感到极其不悦,奶牛们退出了农场,组建了奶牛议会.议会以“每头牛 都可以获得自己想要的”为原则,建立了下面的投票系统: M只到场的奶牛 ...

  8. 623. Add One Row to Tree

    Problem statement Given the root of a binary tree, then value v and depth d, you need to add a row o ...

  9. 【单调队列+二分查找】bzoj 1012: [JSOI2008]最大数maxnumber

    [题意] 维护一个单调递减的q数组,用id数组记录q数组的每个下标对应在原数组的位置,那么id数组一定有单调性(q数组中越靠后,原数组中也靠后),然后二分查找这个数 [AC] #include< ...

  10. centos 7如何配置网络、网卡、ip命令

    Linux网络相关配置文件 Linux网络配置相关的文件根据不同的发行版目录名称有所不同,但大同小异,主要有似下目录或文件. (1)/etc/hostname:主要功能在于修改主机名称. (2)/et ...