HDU1423 Greatest Common Increasing Subsequence (DP优化)
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优化)的更多相关文章
- HDU4512完美队形I && HDU1423 Greatest Common Increasing Subsequence (LCIS)
填坑的时候又到啦,校赛因为不会LCIS所以吃了大亏,这里要补起来.LCIS就是在两个串里找最长上升子序列,相关的博客有很多,这里自己就不写那么多了. http://www.cnblogs.com/ja ...
- HDU1423 Greatest Common Increasing Subsequence
题意 如标题. \(|s1|,|s2| \leq 500\) 分析 既然是dp问题的组合,那么考虑dp. 定义状态f(i,j)表示对第一个序列s1的前i个和第二个序列s2的前j个元素求最长上升公共子序 ...
- HDOJ 1423 Greatest Common Increasing Subsequence(dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1423 思路分析:[问题定义]给定两个序列A[0, 1,..., m]和B[0, 1, ..., n], ...
- 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 ...
- HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】
HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...
- Greatest Common Increasing Subsequence hdu1423
Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- POJ 2127 Greatest Common Increasing Subsequence
You are given two sequences of integer numbers. Write a program to determine their common increasing ...
- 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 ...
- HDU 1423 Greatest Common Increasing Subsequence LCIS
题目链接: 题目 Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
随机推荐
- 第一天python3 封装和解构
封装 将多个值使用逗号分割,组合在一起:本质上,返回一个元组,只是省略了小括号:python特有语法,被很多语言学习和借鉴;比如javascript:t1=(1,2) 定义为元组:t2=1,2 将1和 ...
- angular 变化检测和ngZone
- 一文搞懂│php 中的 DI 依赖注入
目录 什么是 DI / 依赖注入 依赖注入出现的原因 简单的依赖注入 高阶的依赖注入 依赖注入的应用 依赖注入高阶优化 什么是 DI / 依赖注入 依赖注入DI 其实本质上是指对类的依赖通过构造器完成 ...
- 【原创】Python 懂车帝口碑爬虫
本文所有教程及源码.软件仅为技术研究.不涉及计算机信息系统功能的删除.修改.增加.干扰,更不会影响计算机信息系统的正常运行.不得将代码用于非法用途,如侵立删! 懂车帝综合口碑 需求 操作环境 win1 ...
- 设置Windows Server 2022、Win10、Win11自动登录的简单方法-OK
这里介绍自己从使用 Windows Server 2003 到 Windows Server 2022 一直都在使用的自动登录系统的方法,屡试不爽.网上讨论的方法太繁琐,所以共享出来,供大家参考.该方 ...
- WebView2 通过 PuppeteerSharp 实现爬取 王者 壁纸 (案例版)
此案例是<.Net WebView2 项目,实现 嵌入 WEB 页面 Chromium内核>文的续集. 主要是针对WebView2的一些微软自己封装的不熟悉的API,有一些人已经对 Pup ...
- Luogu1382 楼房 (线段树 扫描线)
各种低级错误.jpg,数组开大就过.jpg 线段树离散化扫描线 #include <iostream> #include <cstdio> #include <cstri ...
- Docker 10 镜像原理
参考源 https://www.bilibili.com/video/BV1og4y1q7M4?spm_id_from=333.999.0.0 https://www.bilibili.com/vid ...
- 使用二手 gopro 做行车记录仪
背景 自打开了博客以后,一直在写技术说明文,今天打算写点程序以外的东西换换味口.前段时间在某鱼上以 300 元的价格入手了一套完整的 gopro3+ 运动摄像头,带一张 32G SD 卡,两块备用电池 ...
- 统计 Word 文档字数的方式
描述 欲统计某文档的字数,有两种方式. "审阅"选项卡--"校对"组--字符统计 点击左下角字数统计 审阅查看字数 此步骤较为复杂,在审阅选项卡中可以查询文档的 ...