Acwing272 最长公共上升子序列
题目大意:给定两个大小为n的数组,让你找出最长公共上升子序列的长度。
分析:这是一个比较好的dp题,LIS和LCS两大经典线性dp问题相结合,简称LCIS。
代码(O(n*n*n)写法):
#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e3+;
int a[maxn],b[maxn];
int dp[maxn][maxn];
int main() {
int n;
cin >> n;
for (int i = ; i <= n; i++)
cin >> a[i];
for (int i = ; i <= n; i++)
cin >> b[i];
a[] = b[] = -0x3f3f3f3f;
for (int i = ; i <= n; i++) {
for (int j = ; j <= n; j++) {
if (a[i] == b[j]) {
for (int k = ; k < j; k++) {
if (b[k] < a[i])
dp[i][j] = max(dp[i][j], dp[i - ][k] + );
}
} else dp[i][j] = dp[i - ][j];
}
}
int ans = ;
for (int i = ; i <= n; i++) {
for (int j = ; j <= n; j++) {
ans = max(ans, dp[i][j]);
}
}
cout << ans << endl;
return ;
}
代码(O(n*n)写法):
#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e3+;
int a[maxn],b[maxn];
int dp[maxn][maxn];
int main() {
int n;
cin >> n;
for (int i = ; i <= n; i++)
cin >> a[i];
for (int i = ; i <= n; i++)
cin >> b[i];
a[] = b[] = -0x3f3f3f3f;
for (int i = ; i <= n; i++) {
int val = ;
if (b[] < a[i]) val = dp[i - ][];
for (int j = ; j <= n; j++) {
if (a[i] == b[j])
dp[i][j] = val + ;
else
dp[i][j] = dp[i - ][j];
if (b[j] < a[i])
val = max(val, dp[i - ][j]);
}
}
int ans = ;
for (int i = ; i <= n; i++) {
for (int j = ; j <= n; j++)
ans = max(ans, dp[i][j]);
}
cout << ans << endl;
return ;
}
Acwing272 最长公共上升子序列的更多相关文章
- 题解【AcWing272】最长公共上升子序列
题面 一道线性 DP 好题. 设 \(dp_{i,j}\) 表示在所有 \(a_{1\dots i}\),\(b_{1\dots j}\) 的子序列中,以 \(b_j\) 结尾的最长公共上升子序列的最 ...
- 最长公共上升子序列(codevs 2185)
题目描述 Description 熊大妈的奶牛在小沐沐的熏陶下开始研究信息题目.小沐沐先让奶牛研究了最长上升子序列,再让他们研究了最长公共子序列,现在又让他们要研究最长公共上升子序列了. 小沐沐说,对 ...
- 最长公共上升子序列(LCIS)
最长公共上升子序列慕名而知是两个字符串a,b的最长公共递增序列,不一定非得是连续的.刚开始看到的时候想的是先用求最长公共子序列,然后再从其中找到最长递增子序列,可是仔细想一想觉得这样有点不妥,然后从网 ...
- ZOJ 2432 Greatest Common Increasing Subsequence(最长公共上升子序列+路径打印)
Greatest Common Increasing Subsequence 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...
- POJ 2127 最长公共上升子序列
动态规划法: #include <iostream> #include <cstdio> #include <fstream> #include <algor ...
- [CodeForces10D]LCIS(最长公共上升子序列) - DP
Description 给定两个数列,求最长公共上升子序列,并输出其中一种方案. Input&Output Input 第一行一个整数n(0<n<=500),数列a的长度. 第二行 ...
- 最长递增子序列(lis)最长公共子序列(lcs) 最长公共上升子序列(lics)
lis: 复杂度nlgn #include<iostream> #include<cstdio> using namespace std; ],lis[],res=; int ...
- codevs 2185 最长公共上升子序列
题目链接: codevs 2185 最长公共上升子序列codevs 1408 最长公共子序列 题目描述 Description熊大妈的奶牛在小沐沐的熏陶下开始研究信息题目.小沐沐先让奶牛研究了最长上升 ...
- [ACM_动态规划] UVA 12511 Virus [最长公共递增子序列 LCIS 动态规划]
Virus We have a log file, which is a sequence of recorded events. Naturally, the timestamps are s ...
随机推荐
- C语言:求n(n<10000)以内的所有四叶玫瑰数。-将字符串s1和s2合并形成新的字符串s3,先取出1的第一个字符放入3,再取出2的第一个字符放入3,
//函数fun功能:求n(n<10000)以内的所有四叶玫瑰数并逐个存放到result所指数组中,个数作为返回值.如果一个4位整数等于其各个位数字的4次方之和,则称该数为函数返回值. #incl ...
- Java入门笔记 03-面向对象(中)
介绍:这部分内容主要是介绍和总结封装.继承和多态. 一. 封装:把该隐藏的隐藏起来,把该暴露的暴露出来 封装是指将信息隐藏在对象内部,不允许外部程序直接访问对象内部信息,而是通过该类所提供的方法来实现 ...
- acm数论之旅(转载) -- 快速幂
0和1都不是素数,也不是合数. a的b次方怎么求 pow(a, b)是数学头文件math.h里面有的函数 可是它返回值是double类型,数据有精度误差 那就自己写for循环咯 LL pow(LL a ...
- 一个含有Fibonacci Number的级数
\[\Large\displaystyle \sum_{n=0}^\infty \frac{1}{F_{2n+1}+1}=\frac{\sqrt5}{2}\] \(\Large\mathbf{Proo ...
- CDH
CDH 1.CDH简介 CDH 2.Cloudera Manager的安装 软件下载地址: 链接:https://pan.baidu.com/s/1C5HpiVEOtH_4PjylyJaXvA ...
- pytorch资料
torchvision是独立于pytorch的关于图像操作的一些方便工具库. torchvision的详细介绍在:https://pypi.org/project/torchvision/ torch ...
- Mysql安装 ----> 基于源码包安装
1)基于源码包安装MySQL [root@localhost ~]# rpm -q mysql mysql-server mariadb mairadb-server //检查有没 ...
- 关于MQTT连接的属性
连接相关的属性. 这些属性是MQTT的连接报文中连接标志字, 包含一些用于指定 MQTT 连接行为的参数. 1.清理会话(Clean Session) 客户端和服务端可以保存会话状态,以支持跨网络连接 ...
- springboot例子
@Mapperpublic interface FinancingMapper { @Insert("<script>" + "insert into fin ...
- Caused by: java.lang.NoClassDefFoundError: org/apache/commons/pool2/impl/GenericObjectPoolConfig
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/pool2/impl/GenericObjectPoolConfig at ...