题目大意:给定两个大小为n的数组,让你找出最长公共上升子序列的长度。

分析:这是一个比较好的dp题,LIS和LCS两大经典线性dp问题相结合,简称LCIS。

代码(O(n*n*n)写法):

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 3e3+;
  4. int a[maxn],b[maxn];
  5. int dp[maxn][maxn];
  6. int main() {
  7. int n;
  8. cin >> n;
  9. for (int i = ; i <= n; i++)
  10. cin >> a[i];
  11. for (int i = ; i <= n; i++)
  12. cin >> b[i];
  13. a[] = b[] = -0x3f3f3f3f;
  14. for (int i = ; i <= n; i++) {
  15. for (int j = ; j <= n; j++) {
  16. if (a[i] == b[j]) {
  17. for (int k = ; k < j; k++) {
  18. if (b[k] < a[i])
  19. dp[i][j] = max(dp[i][j], dp[i - ][k] + );
  20. }
  21. } else dp[i][j] = dp[i - ][j];
  22. }
  23. }
  24. int ans = ;
  25. for (int i = ; i <= n; i++) {
  26. for (int j = ; j <= n; j++) {
  27. ans = max(ans, dp[i][j]);
  28. }
  29. }
  30. cout << ans << endl;
  31. return ;
  32. }

代码(O(n*n)写法):

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 3e3+;
  4. int a[maxn],b[maxn];
  5. int dp[maxn][maxn];
  6. int main() {
  7. int n;
  8. cin >> n;
  9. for (int i = ; i <= n; i++)
  10. cin >> a[i];
  11. for (int i = ; i <= n; i++)
  12. cin >> b[i];
  13. a[] = b[] = -0x3f3f3f3f;
  14. for (int i = ; i <= n; i++) {
  15. int val = ;
  16. if (b[] < a[i]) val = dp[i - ][];
  17. for (int j = ; j <= n; j++) {
  18. if (a[i] == b[j])
  19. dp[i][j] = val + ;
  20. else
  21. dp[i][j] = dp[i - ][j];
  22. if (b[j] < a[i])
  23. val = max(val, dp[i - ][j]);
  24. }
  25. }
  26. int ans = ;
  27. for (int i = ; i <= n; i++) {
  28. for (int j = ; j <= n; j++)
  29. ans = max(ans, dp[i][j]);
  30. }
  31. cout << ans << endl;
  32. return ;
  33. }

Acwing272 最长公共上升子序列的更多相关文章

  1. 题解【AcWing272】最长公共上升子序列

    题面 一道线性 DP 好题. 设 \(dp_{i,j}\) 表示在所有 \(a_{1\dots i}\),\(b_{1\dots j}\) 的子序列中,以 \(b_j\) 结尾的最长公共上升子序列的最 ...

  2. 最长公共上升子序列(codevs 2185)

    题目描述 Description 熊大妈的奶牛在小沐沐的熏陶下开始研究信息题目.小沐沐先让奶牛研究了最长上升子序列,再让他们研究了最长公共子序列,现在又让他们要研究最长公共上升子序列了. 小沐沐说,对 ...

  3. 最长公共上升子序列(LCIS)

    最长公共上升子序列慕名而知是两个字符串a,b的最长公共递增序列,不一定非得是连续的.刚开始看到的时候想的是先用求最长公共子序列,然后再从其中找到最长递增子序列,可是仔细想一想觉得这样有点不妥,然后从网 ...

  4. ZOJ 2432 Greatest Common Increasing Subsequence(最长公共上升子序列+路径打印)

    Greatest Common Increasing Subsequence 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...

  5. POJ 2127 最长公共上升子序列

    动态规划法: #include <iostream> #include <cstdio> #include <fstream> #include <algor ...

  6. [CodeForces10D]LCIS(最长公共上升子序列) - DP

    Description 给定两个数列,求最长公共上升子序列,并输出其中一种方案. Input&Output Input 第一行一个整数n(0<n<=500),数列a的长度. 第二行 ...

  7. 最长递增子序列(lis)最长公共子序列(lcs) 最长公共上升子序列(lics)

    lis: 复杂度nlgn #include<iostream> #include<cstdio> using namespace std; ],lis[],res=; int ...

  8. codevs 2185 最长公共上升子序列

    题目链接: codevs 2185 最长公共上升子序列codevs 1408 最长公共子序列 题目描述 Description熊大妈的奶牛在小沐沐的熏陶下开始研究信息题目.小沐沐先让奶牛研究了最长上升 ...

  9. [ACM_动态规划] UVA 12511 Virus [最长公共递增子序列 LCIS 动态规划]

      Virus  We have a log file, which is a sequence of recorded events. Naturally, the timestamps are s ...

随机推荐

  1. 机器学习 — 从mnist数据集谈起

    做了一些简单机器学习任务后,发现必须要对数据集有足够的了解才能动手做一些事,这是无法避免的,否则可能连在干嘛都不知道,而一些官方例程并不会对数据集做过多解释,你甚至连它长什么样都不知道... 以skl ...

  2. DELPHI开发和使用REDIS

    DELPHI开发和使用REDIS REDIS SERVER是独立的存在,支持WINDOWS,LINUXREDIS PUB/SUB  用于聊天 只是其中的一种用法任何消息或其他类型数据 都可以必须安装 ...

  3. 如何在应用程序中使用ML.NET?

    https://www.cnblogs.com/shanyou/p/9190701.html ML.NET以NuGet包的形式提供,可以轻松安装到新的或现有的.NET应用程序中. 该框架采用了用于其他 ...

  4. Vue ElementUI Tree组件 回显问题(设置选择父级时会全选所有的子级,有此业务场景是不适合的)

    业务场景下有这样的问题 业务需求需要保存前端 半选节点 解决方案 let checked = this.$refs.menuTree.getCheckedKeys(); //此方法获取半选节点 let ...

  5. Linux - 重启wifi和网络

    iwconfig sudo ifconfig wlp4s0 down sudo iwconfig wlp4s0 power off sudo ifconfig wlp4s0 up sudo servi ...

  6. Centos 安装 mysql 5.7

    下载mysql yum包 wget http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm 安转软件源 xxx.rpm是刚刚 ...

  7. 解决maven 在intellij IDEA 下载依赖包速度慢的问题

    解决maven 在intellij IDEA 下载依赖包速度慢的问题 参考:https://www.jianshu.com/p/63a593700ebc

  8. Pandas的Categorical Data类型

    pandas从0.15版开始提供分类数据类型,用于表示统计学里有限且唯一性数据集,例如描述个人信息的性别一般就男和女两个数据常用'm'和'f'来描述,有时也能对应编码映射为0和1.血型A.B.O和AB ...

  9. 华水开学第一课&微信支付

    由于疫情的延续,导致我们不能及时开学.只能在网上观看华水开学第一课,但是好像正常开学也没有这个哈哈(不记得了) 昨天没有玩到很晚,12点就睡下.大约半个小时睡着了.定了8点的闹钟.起来的时候那是真的困 ...

  10. python集合操作方法详解

    前言 说集合之前,我们先说一个小例子,假设某公司有五个人喜欢打篮球,五个人喜欢打游戏,问即打游戏有打篮球的人都有哪些? play_basketball = ['a','b','c','d','e'] ...