题目描述

  给定两个整数序列,写一个程序求它们的最长上升公共子序列。

输入格式

  每个序列用两行表示,第一行是长度L,第二行是该序列。

输出格式

  在第一行,输出该LCIS的长度。第二行,输出该LCIS。

输入样例

5

1 4 2 5 -12

4

-12 1 2 4

输出样例

2

1 4

题解

  表面上看起来是个$O(n^{4})$,但实际上可以优化到$O(n^{2})$(貌似还可以用树状数组优化到$O(nlogn)$)

  我们设$dp[i][j]$为以$a_{1}$到$a_{i}$中的一个数和$b_{j}$为结尾的LCIS,容易得到当$a_{i} = b_{j}$时,$dp[i][j] = \underset{1 \leqslant k < j}{max} \left \{ dp[i - 1][k] + 1 \right \}$,否则$dp[i][j] = dp[i - 1][j]$。

  其实我们可以在枚举$i$、$j$的时候顺便维护$\underset{1 \leqslant k < j}{max} \left \{ dp[i - 1][k] + 1 \right \}$,这样就把时间复杂度降到$O(n^{2})$了。

  观察方程,其实我们第一位只会用到$i - 1$和$i$,这里又可以用滚动数组优化。

  1. #include <iostream>
  2.  
  3. #define MAX_N (500 + 5)
  4. #define MAX_M (500 + 5)
  5.  
  6. using namespace std;
  7.  
  8. int n, m;
  9. int a[MAX_N], b[MAX_M];
  10. int dp[MAX_M];
  11. int p[MAX_M];
  12. int ans;
  13.  
  14. void LCIS(int x)
  15. {
  16. if(p[x]) LCIS(p[x]);
  17. cout << b[x] << " ";
  18. return;
  19. }
  20.  
  21. int main()
  22. {
  23. cin >> n;
  24. for(register int i = ; i <= n; ++i)
  25. {
  26. cin >> a[i];
  27. }
  28. cin >> m;
  29. for(register int i = ; i <= m; ++i)
  30. {
  31. cin >> b[i];
  32. }
  33. int pos = , tmp;
  34. for(register int i = ; i <= n; ++i)
  35. {
  36. tmp = ;
  37. for(register int j = ; j <= m; ++j)
  38. {
  39. if(a[i] > b[j] && dp[j] > dp[tmp]) tmp = j;
  40. if(a[i] == b[j])
  41. {
  42. dp[j] = dp[tmp] + ;
  43. p[j] = tmp;
  44. }
  45. }
  46. }
  47. for(register int i = ; i <= m; ++i)
  48. {
  49. if(dp[i] > dp[pos]) pos = i;
  50. }
  51. cout << dp[pos] << "\n";
  52. if(dp[pos]) LCIS(pos);
  53. return ;
  54. }

参考程序

【题解】LCIS的更多相关文章

  1. CH5101 LCIS(最长公共上升子序列) 题解

    每日一题 day16 打卡 Analysis 设F[i,j]表示A[1..i]与B[1..j]并且以B[j]结尾的两段最长公共上升子序列,那么我们可以发现这样的转移 (1)A[i]==B[j]时 F[ ...

  2. BestCoder Round #87 LCIS(dp)

    LCIS 要用dp的思路想这题 [题目链接]LCIS [题目类型]dp &题意: 给定两个序列,求它们的最长公共递增子序列的长度, 并且这个子序列的值是连续的,比如(x,x+1,...,y−1 ...

  3. 8.3 LIS LCS LCIS(完结了==!)

    感觉这个专题真不好捉,伤心了,慢慢啃吧,孩纸 地址http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28195#overview 密码  ac ...

  4. Codeforces Beta Round #10 D. LCIS

    题目链接: http://www.codeforces.com/contest/10/problem/D D. LCIS time limit per test:1 secondmemory limi ...

  5. HDU 1423 Greatest Common Increasing Subsequence LCIS

    题目链接: 题目 Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...

  6. LCIS(线段树区间合并)

    LCIS Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  7. H - The LCIS on the Tree HDU - 4718

    The LCIS on the Tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Oth ...

  8. Codeforces Beta Round #10 D. LCIS 动态规划

    D. LCIS 题目连接: http://www.codeforces.com/contest/10/problem/D Description This problem differs from o ...

  9. CF10D/POJ2127 LCIS解题报告

    题目传送门(洛谷)(CF)(POJ) 前言 期末考试前的最后一篇题解,希望期末考  rp++ 奇怪,为什么在CF上能过的代码到POJ上就 听取WA声一片  (不管了) 题目思路 LCIS模版O(n²) ...

随机推荐

  1. C#设计模式:抽象工厂(Abstract Factory)

    一,抽象工厂模式 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...

  2. ubuntu vim8.1编译安装

    sudo apt-get install libncurses5-dev python-dev python3-dev libgtk-3-dev libatk1.0-dev libbonoboui2- ...

  3. Windows 下 MySQL 备份脚本

    @title MySQL备份脚本 @echo off @echo root@127.0.0.1:3306 set host=127.0.0.1 set port=3306 set user=root ...

  4. 微信小程序(8)--头部导航滑动

    项目需求:实现头部导航,可左右滑动. <view class="top-news"> <view class="self-box"> & ...

  5. Cesium标点

    let startPoint = this.viewer.entities.add( //viewer.entities.add 添加实体的方法 { name: '测量距离', //这个属性跟页面显示 ...

  6. Vue中 let 关键字

    let es6新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效. 不存在变量提升 var命令会发生”变量提升“现象,即变量可以在声明之前使用,值 ...

  7. 【8.0.0_r4】AMS分析(十七)(ActivityManagerService.java下)

    代码位于frameworks/base/services/core/java/com/android/server/am/,一共有七十个文件. Java源码位于package com.android. ...

  8. Linux进程管理——查看内存的工具

    Linux进程管理——查看内存的工具 一查看内存的工具vmstat vmstat命令:虚拟内存信息vmstat [options] [delay [count]]vmstat 2 5 [root@ce ...

  9. 查完数据库order_by后跟[:9]切片取前9位的值

  10. FMX Android ZIP解压中文乱码

    在手机上解压了一个WINDOWS上的压缩文件, 发现中文是乱码的,解决方法如下: 找到System.zip.pas文件 将E := TEncoding.GetEncoding(437);   改为 E ...