CF10D LCIS 最长公共上升子序列
题目描述
This problem differs from one which was on the online contest.
The sequence a1,a2,...,an a_{1},a_{2},...,a_{n} a1,a2,...,an is called increasing, if ai<ai+1 a_{i}<a_{i+1} ai<ai+1 for i<n i<n i<n .
The sequence s1,s2,...,sk s_{1},s_{2},...,s_{k} s1,s2,...,sk is called the subsequence of the sequence a1,a2,...,an a_{1},a_{2},...,a_{n} a1,a2,...,an , if there exist such a set of indexes 1<=i1<i2<...<ik<=n 1<=i_{1}<i_{2}<...<i_{k}<=n 1<=i1<i2<...<ik<=n that aij=sj a_{ij}=s_{j} aij=sj . In other words, the sequence s s s can be derived from the sequence a a a by crossing out some elements.
You are given two sequences of integer numbers. You are to find their longest common increasing subsequence, i.e. an increasing sequence of maximum length that is the subsequence of both sequences.
输入格式
The first line contains an integer n n n ( 1<=n<=500 1<=n<=500 1<=n<=500 ) — the length of the first sequence. The second line contains n n n space-separated integers from the range [0,109] [0,10^{9}] [0,109] — elements of the first sequence. The third line contains an integer m m m ( 1<=m<=500 1<=m<=500 1<=m<=500 ) — the length of the second sequence. The fourth line contains m m m space-separated integers from the range [0,109] [0,10^{9}] [0,109] — elements of the second sequence.
输出格式
In the first line output k k k — the length of the longest common increasing subsequence. In the second line output the subsequence itself. Separate the elements with a space. If there are several solutions, output any.
题意翻译
然后考虑转移方程。
当A[i]!=B[j]时,dp[i][j]=dp[i-1][j]。这里很好理解,A[i]与B[j]都不相等了,自然可以往前推一个。要注意的是这里dp数组第二维的含义,还是以B[j]结尾,所以i->i-1而j不变。
当A[i]==B[j]时,说明AB两串至少有A[i]或者说B[j]这个公共元素了,再次考虑dp数组的定义,以B[j]为结尾的LCIS的长度”,自然想到找到一个末尾小于B[j]的串把B[j]给接上去。因此转移方程可以写成dp[i][j]=max(dp[i-1][k]+1|k:1 to j-1)。当然从决策集合考虑还可以优化成O(n^2),但看这个题的数据范围目前这么写足以过掉。
下面来看存储答案。这里借鉴了洛谷题解的巧妙思想,再创建一个二维数组out,其含义为:out[p][q]代表以B[p]结尾的LCIS的前q个元素。这样一来,在第二重for循环中,当遇到A[i]!=B[j]时,自然不用更新;当A[i]==B[j]时,首先令out[j][1]=b[j],这代表目前LCIS起码有一个元素B[j]。然后第三重循环遍历时遇到可以接上B[j]的,如果比当前dp[i][j]大,在更新dp[i][j]的
同时也更新out[j][1~dp[i-1][k]]。
#include <bits/stdc++.h>
using namespace std;
int n1,n2;
int a[];
int b[];
int dp[][]={};
int out[][];
int main()
{
int i,j,k,m;
int ans=;
cin>>n1;
memset(dp,,sizeof(dp));
for(i=;i<=n1;i++)
{
scanf("%d",&a[i]);
}
cin>>n2;
for(j=;j<=n2;j++)
{
scanf("%d",&b[j]);
}
dp[][]=;
int pos=;
for(i=;i<=n1;i++)
{
for(j=;j<=n2;j++)
{ if(a[i]!=b[j])
{
dp[i][j]=dp[i-][j];//和之前一样的话没必要更新输出串
}
else
{
//
dp[i][j]=;//相等的话最小也为1
out[j][]=b[j];//相等的话最少有一个b[j]
//
for(k=;k<j;k++)//找到最长的进行拼接
{
if(b[k]<b[j])
{
if(dp[i-][k]+>dp[i][j])//大于当前答案的话
{
dp[i][j]=dp[i-][k]+;//更新dp数组
for(m=;m<=dp[i-][k];m++)//更新out数组对应位置的元素
{
out[j][m]=out[k][m];
}
out[j][dp[i][j]]=b[j];//把b[j]加上去
}
}
}
}
ans=max(ans,dp[i][j]);//更新LCIS的长度
if(ans==dp[i][j])pos=j; //找到对应的位置
}
}
cout<<ans<<endl;
for(i=;i<=ans;i++)//输出LCIS
{
cout<<out[pos][i]<<' ';
} return ;
}
CF10D LCIS 最长公共上升子序列的更多相关文章
- [CodeForces10D]LCIS(最长公共上升子序列) - DP
Description 给定两个数列,求最长公共上升子序列,并输出其中一种方案. Input&Output Input 第一行一个整数n(0<n<=500),数列a的长度. 第二行 ...
- LCIS最长公共上升子序列
最长公共上升子序列LCIS,如字面意思,就是在对于两个数列A和B的最长的单调递增的公共子序列. 这道题目是LCS和LIS的综合. 在LIS中,我们通过两重循环枚举当序列以当前位置为结尾时,A序列中当前 ...
- LCIS 最长公共上升子序列问题DP算法及优化
一. 知识简介 学习 LCIS 的预备知识: 动态规划基本思想, LCS, LIS 经典问题:给出有 n 个元素的数组 a[] , m 个元素的数组 b[] ,求出它们的最长上升公共子序列的长度. 例 ...
- LCIS(最长公共上升子序列)Vijos1264神秘的咒语
描述 身为拜月教的高级间谍,你的任务总是逼迫你出生入死.比如这一次,拜月教主就派你跟踪赵灵儿一行,潜入试炼窟底. 据说试炼窟底藏着五行法术的最高法术:风神,雷神,雪妖,火神,山神的咒语.为了习得这些法 ...
- LCIS 最长公共上升子序列
这个博客好久没写了,这几天为了准备清华交叉研究院的夏令营,在复习大一大二ACM训练时的一些基础算法,正好碰到LICS,发现没有写在博客里,那就顺便记录一下好了. 参考链接:http://blog.cs ...
- LCIS(最长公共上升子序列)模板
求出LCIS并输出其路径. 1 #include <iostream> 2 #include <cstdio> 3 #include <string> 4 #inc ...
- CodeForces 10D. LCIS 最长公共上升子序列模板题 + 打印路径
推荐一篇炒鸡赞的blog. 以下代码中有打印路径. #include <algorithm> #include <iostream> #include <cstring& ...
- 【简单dp】poj 2127 Greatest Common Increasing Subsequence【最长公共上升子序列】【模板】
Sample Input 5 1 4 2 5 -12 4 -12 1 2 4 Sample Output 2 1 4 题目:给你两个数字序列,求出这两个序列的最长公共上升子序列.输出最长的长度,并打表 ...
- 最长公共上升子序列(LCIS)
最长公共上升子序列慕名而知是两个字符串a,b的最长公共递增序列,不一定非得是连续的.刚开始看到的时候想的是先用求最长公共子序列,然后再从其中找到最长递增子序列,可是仔细想一想觉得这样有点不妥,然后从网 ...
随机推荐
- LED Magic Light - How Does The LED Light Change Color?
The LED Magic Light states that the color-changing LED is not an LED in the package, but three ...
- pyodbc-的一些说明
cursor的description 可以获得一些关于表的信息 info=cursor.description 此时表中有多少列就有多少个元素,一个元素就是一列的信息(格式是tuple),所以这里的i ...
- css: transform导致文字显示模糊
css: transform导致文字显示模糊 有人认为模糊的原因是:"transform时div的宽度或者高度并不是偶数,偏移 50% 之后,像素点不是整数,和显示像素没有对上". ...
- 6_13古代象形符号(UVa1103)<图的连通块的应用>
给出一幅黑白图像,每行相邻的四个点压缩成一个十六进制的字符.然后还有题中图示的6中古老的字符,按字母表顺序输出这些字符的标号. 输出说明:For each test case, display its ...
- 高内存 高CPU 劣质网络下的测试
内存 先把系统的虚拟内存去掉 (右键我的电脑属性里有的.选择那个无分页文件 虚拟内存在任务管理器就不显示了), 然后机子本身内存不高,开几个网页就满了 CPU cpu可以用鲁大师测试cpu ...
- m大子段和 hdu1024
给出n个数,m个区间: 求选区m个区间的最大值: #include<cstdio> #include<algorithm> #include<math.h> #in ...
- Reinforcement Learning Algorithm 资源
算法源码: https://github.com/ljpzzz/machinelearning https://github.com/imraviagrawal/Reinforcement-Learn ...
- 【音乐欣赏】《99》 - MOB CHOIR
曲名:99 作者:MOB CHOIR [00:00.000] 作曲 : 佐々木淳一 [00:00.150] 作词 : 佐々木淳一 [00:00.450] [00:02.370]If everyone ...
- P & R 9
Floorplan: 要做好floorplan需要掌握哪些知识跟技能? 明确Floorplan 处理的对象:对于数字设计的 Floorplan 来说,它是一个很依赖前后步骤的一个过程,这个可以看作是后 ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019 J Random Access Iterator (树形DP)
每次循环向下寻找孩子时,随机选取一个孩子,设dp[u]为从u出发,不能得出正确答案的概率,则从u出发,走一次的情况下不能得出正确答案的概率是 P = (dp[v1]+dp[v2]+dp[v3]+--d ...