【题解】LCIS
题目描述
给定两个整数序列,写一个程序求它们的最长上升公共子序列。
输入格式
每个序列用两行表示,第一行是长度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$,这里又可以用滚动数组优化。
- #include <iostream>
- #define MAX_N (500 + 5)
- #define MAX_M (500 + 5)
- using namespace std;
- int n, m;
- int a[MAX_N], b[MAX_M];
- int dp[MAX_M];
- int p[MAX_M];
- int ans;
- void LCIS(int x)
- {
- if(p[x]) LCIS(p[x]);
- cout << b[x] << " ";
- return;
- }
- int main()
- {
- cin >> n;
- for(register int i = ; i <= n; ++i)
- {
- cin >> a[i];
- }
- cin >> m;
- for(register int i = ; i <= m; ++i)
- {
- cin >> b[i];
- }
- int pos = , tmp;
- for(register int i = ; i <= n; ++i)
- {
- tmp = ;
- for(register int j = ; j <= m; ++j)
- {
- if(a[i] > b[j] && dp[j] > dp[tmp]) tmp = j;
- if(a[i] == b[j])
- {
- dp[j] = dp[tmp] + ;
- p[j] = tmp;
- }
- }
- }
- for(register int i = ; i <= m; ++i)
- {
- if(dp[i] > dp[pos]) pos = i;
- }
- cout << dp[pos] << "\n";
- if(dp[pos]) LCIS(pos);
- return ;
- }
参考程序
【题解】LCIS的更多相关文章
- CH5101 LCIS(最长公共上升子序列) 题解
每日一题 day16 打卡 Analysis 设F[i,j]表示A[1..i]与B[1..j]并且以B[j]结尾的两段最长公共上升子序列,那么我们可以发现这样的转移 (1)A[i]==B[j]时 F[ ...
- BestCoder Round #87 LCIS(dp)
LCIS 要用dp的思路想这题 [题目链接]LCIS [题目类型]dp &题意: 给定两个序列,求它们的最长公共递增子序列的长度, 并且这个子序列的值是连续的,比如(x,x+1,...,y−1 ...
- 8.3 LIS LCS LCIS(完结了==!)
感觉这个专题真不好捉,伤心了,慢慢啃吧,孩纸 地址http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28195#overview 密码 ac ...
- Codeforces Beta Round #10 D. LCIS
题目链接: http://www.codeforces.com/contest/10/problem/D D. LCIS time limit per test:1 secondmemory limi ...
- HDU 1423 Greatest Common Increasing Subsequence LCIS
题目链接: 题目 Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- LCIS(线段树区间合并)
LCIS Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- 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 ...
- Codeforces Beta Round #10 D. LCIS 动态规划
D. LCIS 题目连接: http://www.codeforces.com/contest/10/problem/D Description This problem differs from o ...
- CF10D/POJ2127 LCIS解题报告
题目传送门(洛谷)(CF)(POJ) 前言 期末考试前的最后一篇题解,希望期末考 rp++ 奇怪,为什么在CF上能过的代码到POJ上就 听取WA声一片 (不管了) 题目思路 LCIS模版O(n²) ...
随机推荐
- C#设计模式:抽象工厂(Abstract Factory)
一,抽象工厂模式 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...
- ubuntu vim8.1编译安装
sudo apt-get install libncurses5-dev python-dev python3-dev libgtk-3-dev libatk1.0-dev libbonoboui2- ...
- 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 ...
- 微信小程序(8)--头部导航滑动
项目需求:实现头部导航,可左右滑动. <view class="top-news"> <view class="self-box"> & ...
- Cesium标点
let startPoint = this.viewer.entities.add( //viewer.entities.add 添加实体的方法 { name: '测量距离', //这个属性跟页面显示 ...
- Vue中 let 关键字
let es6新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效. 不存在变量提升 var命令会发生”变量提升“现象,即变量可以在声明之前使用,值 ...
- 【8.0.0_r4】AMS分析(十七)(ActivityManagerService.java下)
代码位于frameworks/base/services/core/java/com/android/server/am/,一共有七十个文件. Java源码位于package com.android. ...
- Linux进程管理——查看内存的工具
Linux进程管理——查看内存的工具 一查看内存的工具vmstat vmstat命令:虚拟内存信息vmstat [options] [delay [count]]vmstat 2 5 [root@ce ...
- 查完数据库order_by后跟[:9]切片取前9位的值
- FMX Android ZIP解压中文乱码
在手机上解压了一个WINDOWS上的压缩文件, 发现中文是乱码的,解决方法如下: 找到System.zip.pas文件 将E := TEncoding.GetEncoding(437); 改为 E ...