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 ...
随机推荐
- CSS-禁止文本被选中
pc端: .not-select{ -moz-user-select:none; /*火狐*/ -webkit-user-select:none; /*webkit浏览器*/ -ms-user-sel ...
- 「CQOI2015」任务查询系统
「CQOI2015」任务查询系统 传送门 好像也是板子题??? 区间修改,单点查询,考虑差分. 然后每次查询时就直接在对应的主席树上二分即可. 参考代码: #include <cstdio> ...
- Steam 游戏 《Crashlands(崩溃大陆)》修改器制作-[先使用CE写,之后有时间的话改用CheatMaker](2020年寒假小目标12)
日期:2020.02.15 博客期:155 星期六 [温馨提示]: 只是想要修改器的网友,可以直接点击此链接下载: 只是想拿CT文件的网友,可以直接点击此链接下载: 没有博客园账号的网友,可以将页面下 ...
- December 28th, Week 52nd Saturday, 2019
If you start at the bottom, pay your dues, life here can be a dream come true. 只要你从头开始,脚踏实地,梦想是可以成真的 ...
- 【渗透测试】Msf提权步骤
1.生成反弹木马(脚本,执行程序) msfvenom -p windows/meterpreter/reverse_tcp LHOST=<Your IP Address> LPORT=&l ...
- 战争游戏OverTheWire:Bandit(一)
一个用来熟悉linux命令的游戏: Level0 告诉我们使用ssh连接网址,用户名和密码皆为bandit0.使用Xshell或者linux连接都可以 我使用的是Xshell5: Level0-> ...
- 无线连接网络-FAST SSID Change
这篇随笔主要记录的是Apple设备连接思科无线可能出现的问题,尤其是在思科WLC3504下部署的无线网络,这种故障体现的尤为明显. For Single SSID To support Apple i ...
- Codeforces Round #619 (Div. 2) B. Motarack's Birthday
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motar ...
- Linux vim中方向键变成字母的问题
使用Ubuntu Desktop 18.04 时 发现 vim 在编辑模式的时候,方向键变成了字母ABCD. 原因: Ubuntu预装的是vim tiny版本,安装vim full版本即可解决. 1. ...
- C语言笔记 12_可变参数&内存管理&命令行参数
可变参数 有时,您可能会碰到这样的情况,您希望函数带有可变数量的参数,而不是预定义数量的参数.C 语言为这种情况提供了一个解决方案,它允许您定义一个函数,能根据具体的需求接受可变数量的参数.下面的实例 ...