LIS和LCS的结合。

容易写出方程,复杂度是nm2,但我们可以去掉一层没有必要的枚举,用一个变量val记录前一阶段的最优解,这样优化成nm。

1<=k<j,j增加1,k的上界也增加1,就可以考虑用变量优化去掉一层循环。

 1 #include<cstdio>
2 #include<cstring>
3 #include<algorithm>
4 using namespace std;
5 const int maxn=505;
6 int n,m,T,ans;
7 int a[maxn],b[maxn],dp[maxn][maxn];
8
9 int solve(int *a,int n,int *b,int m){
10 ans=0;
11 memset(dp,0,sizeof(dp));
12 for(int i=1;i<=n;i++){
13 int val=0;//记录决策集合S(i,j)中dp[i-1][k]的最大值
14 for(int j=1;j<=m;j++){
15 if(a[i]!=b[j]) dp[i][j]=dp[i-1][j];
16 else dp[i][j]=val+1;
17 if(b[j]<a[i]) val=max(val,dp[i-1][j]);
18 ans=max(dp[i][j],ans);
19 }
20 }
21 return ans;
22 }
23
24 int main(){
25 scanf("%d",&T);
26 while(T--){
27 scanf("%d",&n);
28 for(int i=1;i<=n;i++)
29 scanf("%d",&a[i]);
30 scanf("%d",&m);
31 for(int j=1;j<=m;j++)
32 scanf("%d",&b[j]);
33 printf("%d\n",solve(a,n,b,m));
34 if(T) printf("\n");
35 }
36 return 0;
37 }

HDU1423 Greatest Common Increasing Subsequence (DP优化)的更多相关文章

  1. HDU4512完美队形I && HDU1423 Greatest Common Increasing Subsequence (LCIS)

    填坑的时候又到啦,校赛因为不会LCIS所以吃了大亏,这里要补起来.LCIS就是在两个串里找最长上升子序列,相关的博客有很多,这里自己就不写那么多了. http://www.cnblogs.com/ja ...

  2. HDU1423 Greatest Common Increasing Subsequence

    题意 如标题. \(|s1|,|s2| \leq 500\) 分析 既然是dp问题的组合,那么考虑dp. 定义状态f(i,j)表示对第一个序列s1的前i个和第二个序列s2的前j个元素求最长上升公共子序 ...

  3. HDOJ 1423 Greatest Common Increasing Subsequence(dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1423 思路分析:[问题定义]给定两个序列A[0, 1,..., m]和B[0, 1, ..., n], ...

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

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

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

  6. Greatest Common Increasing Subsequence hdu1423

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

  7. POJ 2127 Greatest Common Increasing Subsequence

    You are given two sequences of integer numbers. Write a program to determine their common increasing ...

  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. HDU 1423 Greatest Common Increasing Subsequence LCIS

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

随机推荐

  1. WPF 实现用户头像选择器

    制作一个用户头像选择器仿 WeGame 制作一个用户头像选择Canvas为父控件所实现,展示图片使用Image,Path当作上方的蒙版; Canvas:主要用途方便移动Image,设置ClipToBo ...

  2. 论文解读(JKnet)《Representation Learning on Graphs with Jumping Knowledge Networks》

    论文信息 论文标题:Representation Learning on Graphs with Jumping Knowledge Networks论文作者:Keyulu Xu, Chengtao ...

  3. SkiaSharp 之 WPF 自绘 粒子花园(案例版)

    此案例包含了简单的碰撞检测,圆形碰撞检测方法,也可以说是五环弹球的升级版,具体可以根据例子参考. 粒子花园 这名字是案例的名字,效果更加具有科技感,很是不错,搞搞做成背景特效也是不错的选择. Wpf ...

  4. 从零开始Blazor Server(7)--使用Furion权限验证

    序 上面两篇我们讲了怎么用OnNavigateAsync来验证权限,又写了怎么用策略来验证权限. 其实我们既然集成了Fution,就可以用Furion带的方式来验证. 创建AdminHandler 我 ...

  5. React报错之react component changing uncontrolled input

    正文从这开始~ 总览 当input的值被初始化为undefined,但后来又变更为一个不同的值时,会产生"A component is changing an uncontrolled in ...

  6. while 循环、do- while 循环 和 for 循环之间的那点事

    C语言自学之三种循环比较 使用循环计算1-2+3-4+5-6+--100的值?    在编辑器中给出了三种循环体结构的部分代码,请选择合适的循环结构补全代码实现此功能.    运行结果为: sum=- ...

  7. Apache DolphinScheduler 需要的sudo,还可以这么玩,长见识了!

    Apache DolphinScheduler(incubator)需要的sudo,还可以这么玩,长见识了! 在新一代大数据任务调度 - Apache DolphinScheduler(以下简称dol ...

  8. Docker 15 Compose

    参考源 https://www.bilibili.com/video/BV1og4y1q7M4?spm_id_from=333.999.0.0 https://www.bilibili.com/vid ...

  9. 275. H 指数 II--Leetcode_二分

    来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/h-index-ii 著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请注明出处. 题目的大意是 ...

  10. 管理 MongoDB 用户和权限

    创建用户 创建用户的函数是:db.createUser(). 创建用户时,需要为该用户添加权限.可添加的权限以及说明: 权限 作用 read 允许用户读取指定数据库. readWrite 允许用户读写 ...