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 ...
随机推荐
- echart 重新加载
myechart2.clear(); myechart2.setOption(options[1]);
- 今天启动项目的时候报了一个错MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk.
从报错信息看应该是内存问题导致不能持久化到硬盘,在网上找到了一个解决方法: Redis被配置为保存数据库快照,但它目前不能持久化到硬盘.用来修改集合数据的命令不能用.请查看Redis日志的详细错误信息 ...
- pb加密转换成C#
本来想把PB 的函数封装成COM组件调用,结果怎么都搞不定,只能讲代码搞出来换种语言实现了. string s_ret integer i_first,i_second,i_third,i_fourt ...
- AOP的基本认识
一.AOP的概念 AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善. ...
- PyQt5窗口操作大全
1.多窗口交互-使用信号与槽函数'''如果一个窗口和一个窗口交互,尽量不要访问窗口B的控件:应该访问与信号绑定的槽函数,从而降低窗口之间的耦合度 例:如果A直接访问B窗口的控件,一旦B窗口的控件发生改 ...
- windows下如何快速删除大文件
rmdir 磁盘:\文件夹的名字 /s /q; eg:rmdir E:\vue_workspace\KB\day08 /s/q /S 表示除目录本身外,还将删除指定目录下的所有子目录和文件. ...
- 吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)
Matplotlib 可能是 Python 2D-绘图领域使用最广泛的套件.它能让使用者很轻松地将数据图形化,并且提供多样化的输出格式. from pylab import * size = 128, ...
- 使用Gogs搭建自己的Git服务--windows
Gogs介绍 官方网站:传送门... 使用Gogs可以搭建一个自己的私有Git服务. 有时候我们有一些有些不想公开的私人小项目或者练习项目,源码想放在GitHub.码云(一直觉得这名字有点蹭知名度)这 ...
- gym102220H 差分+树状数组(区间修改和输出)
这题目很有意思,让我学会了树状数组的差分,更加深刻理解了树状数组 树状数组的差分写法 void add(int x,int k) { for (int i = x;i <= n;i += low ...
- mybatis 无效字符
只想说 是真的坑啊!!!!!sql就是对 但就是报错无效字符..... 一.sql后有 “:”,我觉得这个应该坑了很多人了 二.标签后有类似于空格的东西(我也不知道是tab还是空格,反正删完就对了) ...