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 ...
随机推荐
- 机器学习 — 从mnist数据集谈起
做了一些简单机器学习任务后,发现必须要对数据集有足够的了解才能动手做一些事,这是无法避免的,否则可能连在干嘛都不知道,而一些官方例程并不会对数据集做过多解释,你甚至连它长什么样都不知道... 以skl ...
- DELPHI开发和使用REDIS
DELPHI开发和使用REDIS REDIS SERVER是独立的存在,支持WINDOWS,LINUXREDIS PUB/SUB 用于聊天 只是其中的一种用法任何消息或其他类型数据 都可以必须安装 ...
- 如何在应用程序中使用ML.NET?
https://www.cnblogs.com/shanyou/p/9190701.html ML.NET以NuGet包的形式提供,可以轻松安装到新的或现有的.NET应用程序中. 该框架采用了用于其他 ...
- Vue ElementUI Tree组件 回显问题(设置选择父级时会全选所有的子级,有此业务场景是不适合的)
业务场景下有这样的问题 业务需求需要保存前端 半选节点 解决方案 let checked = this.$refs.menuTree.getCheckedKeys(); //此方法获取半选节点 let ...
- Linux - 重启wifi和网络
iwconfig sudo ifconfig wlp4s0 down sudo iwconfig wlp4s0 power off sudo ifconfig wlp4s0 up sudo servi ...
- Centos 安装 mysql 5.7
下载mysql yum包 wget http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm 安转软件源 xxx.rpm是刚刚 ...
- 解决maven 在intellij IDEA 下载依赖包速度慢的问题
解决maven 在intellij IDEA 下载依赖包速度慢的问题 参考:https://www.jianshu.com/p/63a593700ebc
- Pandas的Categorical Data类型
pandas从0.15版开始提供分类数据类型,用于表示统计学里有限且唯一性数据集,例如描述个人信息的性别一般就男和女两个数据常用'm'和'f'来描述,有时也能对应编码映射为0和1.血型A.B.O和AB ...
- 华水开学第一课&微信支付
由于疫情的延续,导致我们不能及时开学.只能在网上观看华水开学第一课,但是好像正常开学也没有这个哈哈(不记得了) 昨天没有玩到很晚,12点就睡下.大约半个小时睡着了.定了8点的闹钟.起来的时候那是真的困 ...
- python集合操作方法详解
前言 说集合之前,我们先说一个小例子,假设某公司有五个人喜欢打篮球,五个人喜欢打游戏,问即打游戏有打篮球的人都有哪些? play_basketball = ['a','b','c','d','e'] ...