传送门

Luogu

解题思路

首先考虑怎么求方案,这样才可能会输出方案。

考虑 \(\text{DP}\)。

设 \(f[i][j]\) 表示在 \(a\) 序列中选择一个 \([1...i]\) 的子序列子序列 \(b[1...j]\) 匹配得到的最长LCIS(其中 \(b[j]\) 强制被选)。

有一个很显然的 \(O(n^3)\) 转移:

当 \(a_i = b_j\) 时:\(f[i][j] = \max\limits_{1\le k < j \text{且} b_k < b_j}\left\{f[i - 1][k] + 1\right\}\)

当 \(a_i \neq b_j\) 时:\(f[i][j] = f[i - 1][j]\)

这样子转移显然是没错的,但要是 \(1\le N \le 10^3\) 呢?

其实转移可以做到 \(O(n^2)\) 。

仔细想一想就可以发现这样做的转移是具有决策单调性的,我们每次进行第一种转移时,决策集合总是不断扩大的,我们大可不必每次都扫一遍取 \(\max\) 只要动态的维护一下决策集合中的最优决策点就好了。

然后再考虑输出方案。

我们设一个和 \(f\) 数组并存的 \(p\) 数组,每次成功转移时就更新一下 \(p\) ,最后就顺着 \(p\) 数组往前跳倒序输出就好了。

细节注意事项

  • 咕咕咕

参考代码

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while (!isdigit(c)) f |= (c == '-'), c = getchar();
while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
s = f ? -s : s;
} const int _ = 500 + 10; int n, m, a[_], b[_];
int f[_][_], p[_][_]; inline void print(int id)
{ if (id) print(p[n][id]), printf("%d ", b[id]); } int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
read(n); for (rg int i = 1; i <= n; ++i) read(a[i]);
read(m); for (rg int i = 1; i <= m; ++i) read(b[i]);
a[0] = b[0] = -1;
for (rg int i = 1; i <= n; ++i)
for (rg int j = 1; j <= m; ++j) {
if (a[i] != b[j]) {
f[i][j] = f[i - 1][j], p[i][j] = p[i - 1][j];
} else {
for (rg int k = 0; k < j; ++k)
if (f[i][j] < f[i - 1][k] + 1 && b[k] < b[j])
f[i][j] = f[i - 1][k] + 1, p[i][j] = k;
}
}
int id = 0, _max = 0;
for (rg int i = 1; i <= m; ++i)
if (_max < f[n][i])
_max = f[n][i], id = i;
printf("%d\n", _max), print(id);
return 0;
}

完结撒花 \(qwq\)

「CF10D」LCIS的更多相关文章

  1. 「译」JUnit 5 系列:条件测试

    原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...

  2. 「译」JUnit 5 系列:扩展模型(Extension Model)

    原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...

  3. JavaScript OOP 之「创建对象」

    工厂模式 工厂模式是软件工程领域一种广为人知的设计模式,这种模式抽象了创建具体对象的过程.工厂模式虽然解决了创建多个相似对象的问题,但却没有解决对象识别的问题. function createPers ...

  4. 「C++」理解智能指针

    维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...

  5. 「JavaScript」四种跨域方式详解

    超详细并且带 Demo 的 JavaScript 跨域指南来了! 本文基于你了解 JavaScript 的同源策略,并且了解使用跨域跨域的理由. 1. JSONP 首先要介绍的跨域方法必然是 JSON ...

  6. 「2014-5-31」Z-Stack - Modification of Zigbee Device Object for better network access management

    写一份赏心悦目的工程文档,是很困难的事情.若想写得完善,不仅得用对工具(use the right tools),注重文笔,还得投入大把时间,真心是一件难度颇高的事情.但,若是真写好了,也是善莫大焉: ...

  7. 「2014-3-18」multi-pattern string match using aho-corasick

    我是擅(倾)长(向)把一篇文章写成杂文的.毕竟,写博客记录生活点滴,比不得发 paper,要求字斟句酌八股结构到位:风格偏杂文一点,也是没人拒稿的.这么说来,arxiv 就好比是 paper 世界的博 ...

  8. 「2014-3-17」C pointer again …

    记录一个比较基础的东东-- C 语言的指针,一直让人又爱又恨,爱它的人觉得它既灵活又强大,恨它的人觉得它太过于灵活太过于强大以至于容易将人绕晕.最早接触 C 语言,还是在刚进入大学的时候,算起来有好些 ...

  9. 「2014-3-13」Javascript Engine, Java VM, Python interpreter, PyPy – a glance

    提要: url anchor (ajax) => javascript engine (1~4 articles) => java VM vs. python interpreter =& ...

随机推荐

  1. zk的单机部署,与客户端的使用

    下载zk wget https://archive.apache.org/dist/zookeeper/stable/apache-zookeeper-3.5.5-bin.tar.gz 安装jdk t ...

  2. mysql修改字符集为utf8

    https://zhidao.baidu.com/question/1642165712897935220.html

  3. Cisco AP-Flexconnect配置结果

    一个部署Flexconnect AP(印度)注册到远端WLC(上海)的例子:1.连接AP的交换机接口的配置: nterface GigabitEthernet0/4switchport access ...

  4. 安装Ubuntu后的一些配置

    Ubuntu安装的一些配置 搜狗拼音的安装 卸载ibus和它的配置, 卸载顶部面板的键盘指示 sudo apt remove ibus sudo apt purge ibus sudo apt rem ...

  5. Springboot三层架构

    control调用service调用dao

  6. [方法]季节调整与hp滤波方法

    进行时间序列的数据分析时,季节因素调整与hp滤波是进行数据处理与准备工作的常见必要环节.本文简要梳理季节调整与hp滤波的应用场景,以及在Python中的实现方法. 1. 季节调整方法 季节调整的目的是 ...

  7. 使用maven搭建web项目

    在pom.xml中添加java ee相关的三个依赖包:<scope> jar的有效范围 provided 表示编译期生效,不会打包发布到 tomcat 中 <properties&g ...

  8. Ubuntu 16.04 安装Redis服务器端

    ~ sudo apt-get install redis-server 安装完成后,Redis服务器会自动启动,我们检查Redis服务器程序 检查Redis服务器系统进程 ~ ps -aux|grep ...

  9. SpringBoot中普通类无法通过@Autowired自动注入Service、dao等bean解决方法

    无法注入原因: 有的时候我们有一些类并不想注入Spring容器中,有Spring容器实例化,但是我们又想使用Spring容器中的一些对象,所以就只能借助工具类来获取了 工具类: package com ...

  10. Codeforces 599D:Spongebob and Squares

    D. Spongebob and Squares time limit per test 2 seconds memory limit per test 256 megabytes input sta ...