Greatest Common Increasing Subsequenc

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
 
题意:求最长公共上升子序列。
做法:设f[i][j]表示以b[j]结尾的最长公共上升子序列长度。那么有f[i][j]=f[i-1][j],f[i][j]=max(f[i-1][k]){a[i]=b[j],a[i]>b[k]}
这样直接枚举需要三重,虽然对于本题似乎并不会超时(我不知道有几组数据。。)?但我们可以寻求更好的做法。
可以想到,在一重循环i中,a[i]>b[k]中的a[i]是固定的,也就是说,对于循环k,每次长度都只加1,实际上我们可以用一个 变量来维护当前
的j是否满足决策条件 val=max(val,f[i-1][j])(a[i]>b[j]),这样就直接降了一维复杂度。
PS:本题输出格式坑(每个输出间空两行)。
 #include <cstdio>
#include <iostream>
#define N 607
#include <cstring>
using namespace std;
int n,m,T,ans;
int f[N][N],a[N],b[N]; inline int max(int a,int b) {return a>b?a:b;} void Init(){
scanf("%d",&n);
for (int i=;i<=n;i++) scanf("%d",&a[i]);
scanf("%d",&m);
for (int i=;i<=m;i++) scanf("%d",&b[i]);
} void Dp(){
memset(f,,sizeof(f));
for (int i=;i<=n;i++){
int val=;
if (b[]<a[i]) val=f[i-][];
for (int j=;j<=m;j++){
if (a[i]==b[j]) f[i][j]=val+;
else f[i][j]=f[i-][j];
if (a[i]>b[j]) val=max(val,f[i-][j]);
}
}
ans=;
for (int i=;i<=n;i++) ans=max(f[n][i],ans);
} int main(){
scanf("%d",&T);
for(;T--;){
Init();
Dp();
printf("%d\n",ans);
if (T) printf("\n");
}
}
 

HDU 1423 Greatest Common Increasing Subsequence(LCIS)的更多相关文章

  1. 1423 Greatest Common Increasing Subsequence (LCIS)

    讲解摘自百度; 最长公共上升子序列(LCIS)的O(n^2)算法? 预备知识:动态规划的基本思想,LCS,LIS.? 问题:字符串a,字符串b,求a和b的LCIS(最长公共上升子序列).? 首先我们可 ...

  2. HDUOJ ---1423 Greatest Common Increasing Subsequence(LCS)

    Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536 ...

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

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

  4. HDU 1423 Greatest Common Increasing Subsequence LCIS

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

  5. HDU 1423 Greatest Common Increasing Subsequence(LICS入门,只要求出最长数)

    Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536 ...

  6. HDU 1423 Greatest Common Increasing Subsequence

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

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

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

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

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

  9. 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 ...

随机推荐

  1. CodeForces - 95B

    Petya loves lucky numbers. Everybody knows that positive integers are lucky if their decimal represe ...

  2. CentOS6.5安装配置详解

    1. 环境要求 VMWare软件: CentOS6.5对应的iso镜像文件(位数对应个人计算机位数). 2. 安装步骤 打开VMWare,文件->新建虚拟机(以下几步默认跳过即可) 命名和选择安 ...

  3. GraphQL实战经验和性能问题的解决方案

    在现在的公司使用GraphQL有一段时间了. 现公司从创立之后的很长一段时间内是纯PHP的技术栈,前端.后端都在PHP代码中糅合在一起.新功能越加越多,页面越来越复杂之后,那些混在在PHP代码中的HT ...

  4. ASP.NET相关

    1.委托:把一个方法当作参数传到另一个方法中 扩展方法:1.静态类 2.静态方法 3.this关键字 using System; using System.Collections.Generic; u ...

  5. 【Unity3D/C#】利用IEnumerable<>和yield产生斐波那契数列

    using UnityEngine; using System.Collections; using System.Collections.Generic; public class YieldTes ...

  6. eclipse 中安装spring tool suite 插件100%成功率

    自己曾在学习spring时,在eclipse EE中安装springsourceTool Suite插件浪费了很多时间,不管是离线,在线还是在eclipse marketplace中安装,尝试了许多方 ...

  7. cf1042F. Leaf Sets(贪心)

    题意 题目链接 给出一棵树,删除一些边,使得任意联通块内的任意点距离不超过$k$ sol 考场上想的贪心是对的:考虑一棵子树,如果该子树内最深的两个节点的距离相加$>k$就删掉最深的那个点,向上 ...

  8. html的语法 3

    <html> <head> <title>这是第一节课网页标题</title> <!--meta charset="UTF-8" ...

  9. 【MATLAB】设定坐标的轴的范围

    set(gca,'XLim',[0 1.5]);%X轴的数据显示范围set(gca,'XTick',[0:0.1:1.5]);%设置要显示坐标刻度set(gca,'XTickLabel',[0:0.1 ...

  10. 真实场景中WebRTC 用到的服务 STUN, TURN 和 signaling

    FQ收录转自:WebRTC in the real world: STUN, TURN and signaling WebRTC enables peer to peer communication. ...