「CF10D」LCIS
传送门
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的更多相关文章
- 「译」JUnit 5 系列:条件测试
原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...
- 「译」JUnit 5 系列:扩展模型(Extension Model)
原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...
- JavaScript OOP 之「创建对象」
工厂模式 工厂模式是软件工程领域一种广为人知的设计模式,这种模式抽象了创建具体对象的过程.工厂模式虽然解决了创建多个相似对象的问题,但却没有解决对象识别的问题. function createPers ...
- 「C++」理解智能指针
维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...
- 「JavaScript」四种跨域方式详解
超详细并且带 Demo 的 JavaScript 跨域指南来了! 本文基于你了解 JavaScript 的同源策略,并且了解使用跨域跨域的理由. 1. JSONP 首先要介绍的跨域方法必然是 JSON ...
- 「2014-5-31」Z-Stack - Modification of Zigbee Device Object for better network access management
写一份赏心悦目的工程文档,是很困难的事情.若想写得完善,不仅得用对工具(use the right tools),注重文笔,还得投入大把时间,真心是一件难度颇高的事情.但,若是真写好了,也是善莫大焉: ...
- 「2014-3-18」multi-pattern string match using aho-corasick
我是擅(倾)长(向)把一篇文章写成杂文的.毕竟,写博客记录生活点滴,比不得发 paper,要求字斟句酌八股结构到位:风格偏杂文一点,也是没人拒稿的.这么说来,arxiv 就好比是 paper 世界的博 ...
- 「2014-3-17」C pointer again …
记录一个比较基础的东东-- C 语言的指针,一直让人又爱又恨,爱它的人觉得它既灵活又强大,恨它的人觉得它太过于灵活太过于强大以至于容易将人绕晕.最早接触 C 语言,还是在刚进入大学的时候,算起来有好些 ...
- 「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 =& ...
随机推荐
- 【代码学习】PYTHON中的静态方法和类方法
一.类方法 是类对象所拥有的方法,需要用修饰器@classmethod来标识其为类方法,对于类方法,第一个参数必须是类对象,一般以cls作为第一个参数(当然可以用其他名称的变量作为其第一个参数,但是大 ...
- 实现简单HttpServer案例
<html> <head> <title>第一个表单</title> </head> <body> <pre> me ...
- 20 JavaScript随机&逻辑&比较&类型转换
JavaScript 随机 Math.random(): 返回0~1之间的随机数,包括0不包括1 Math.floor():下舍入.Math.floor(2.9) = 2.Math.floor(Mat ...
- Cisco AP-Flexconnect配置结果
一个部署Flexconnect AP(印度)注册到远端WLC(上海)的例子:1.连接AP的交换机接口的配置: nterface GigabitEthernet0/4switchport access ...
- arcPy实现要素图层数据的复制(选择特定字段填写属性)
>>> import arcpy>>> fc=r"D:\楚雄州数据\testdata.gdb">>> editor=arcpy ...
- PB开启源码文件
下载的源码没有pbw文件,新建workspace,然后new Target选existing application
- 十二 Spring的AOP开发入门,整合Junit单元测试(AspectJ的XML方式)
创建web项目,引入jar包 引入Spring配置文件
- springboot 重写 AuthorizationFilter
原文链接:https://www.cnblogs.com/zeussbook/p/10778532.html
- C++启动和关闭外部exe
转载:https://www.cnblogs.com/Sketch-liu/p/7277130.html 1.WinExec( lpCmdLine: LPCSTR; {文件名和参数; 如没指定路径会 ...
- Android 记住密码和自动登录界面的实现(SharedPreferences 的用法)
原文:http://blog.csdn.net/liuyiming_/article/details/7704923 SharedPreferences介绍: SharedPreferences是An ...