传送门

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. Vim:Vim入门级配置

    转:https://vimjc.com/vimrc-config.html Vim配置文件.vimrc Vim编辑器相关的所有功能开关都可以通过.vimrc文件进行设置. .vimrc配置文件分系统配 ...

  2. flask_migrate

    flask_migrate 1.      flask_migrate doc: https://flask-migrate.readthedocs.io/en/latest/ 1.1.    简介 ...

  3. ZOJ007 Numerical Summation of a Series(纯数学)

    #include<bits/stdc++.h> using namespace std; int main() { double i; double k; for(i=0.000;i-2. ...

  4. WinForm开发(1)——DataGridView控件(1)——C# DataGridView控件用法介绍

    DataGridView控件在实际应用中非常实用,特别需要表格显示数据时.可以静态绑定数据源,这样就自动为DataGridView控件添加相应的行.假如需要动态为DataGridView控件添加新行, ...

  5. scala基础API

    1.对象和json转换 1.1 json取值 val json:JSONObject = JSON.parseObject(jsonMsg:String)val message:String = js ...

  6. 【原】Django总结

    centos7下部署django详细步骤:https://www.cnblogs.com/djangocn/p/9538551.html 快速入门:https://www.cnblogs.com/ze ...

  7. 使用docker构建第一个spring boot项目

    在看了一些简单的docker命令之后 打算自己尝试整合一下docker+spring boot项目本文是自己使用docker+spring boot 发布一个项目1.docker介绍 docke是提供 ...

  8. POJ 3258 River Hopscotch(二分答案)

    嗯... 题目链接:http://poj.org/problem?id=3258 一道很典型的二分答案的题目,和跳石头太像了!! 这道题的题目很显然,求最小中的最大值,注意这道题石头的位置不是从小到大 ...

  9. druid监控sql完整版

    利用Druid实现应用和SQL监控 一.关于Druid Druid是一个JDBC组件,它包括三部分: DruidDriver 代理Driver,能够提供基于Filter-Chain模式的插件体系. D ...

  10. 【转】Node.js到底是用来做什么的

    Node.js的到底是用来做什么的 在阐述之前我想放一个链接,这是国外的一个大神,对于node.js非常好的一篇介绍的文章,英文比较好的朋友可以直接去阅读,本文也很大程度上参考了这篇文章,也同时感谢知 ...