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的最长公共递增序列,不一定非得是连续的.刚开始看到的时候想的是先用求最长公共子序列,然后再从其中找到最长递增子序列,可是仔细想一想觉得这样有点不妥,然后从网 ...
随机推荐
- 问题 I: 数字分组2
问题 I: 数字分组2 时间限制: 1 Sec 内存限制: 128 MB[命题人:admin] 题目描述 已知一堆魔法石的重量,问如何分成两堆,使得它们质量和之差最大,但不能大于(可以等于)这些数中 ...
- UVA 12097 Pie(二分答案)
题目链接: 这道题虽然不是一道典型的二分答案题,但同样也可以用二分答案来做. 来二分面积为$area$的派,然后看看条件是否矛盾. 与其矛盾的便是$f+1$个人是否每个人都会有. 一个半径为$r$的派 ...
- Win10 系统运行VsCode出现白屏的问题(亲测有效)
Win10 系统运行VsCode出现白屏的问题(亲测有效) 新买的本本,昨天VScode运行还正常,今天打开一直白屏,什么都没有,只有几个小格格,也不是卡死的那种,可以轻松关闭, 刚开始以为版本问题, ...
- LEETCODE80. 删除排序数组中的重复项
俺的: class Solution: def removeDuplicates(self, nums: List[int]) -> int: if(len(nums)==0): return ...
- RESTful api设计风格
简介 REST(Representational State Transfer):表象层状态转变 RESTful对api进行规范和约束,使得api统一规范,增强api的可读性,便于开发. 设计 ...
- MyBatis(6)——分页的实现
分页的实现 a)通过mysql的分页查询语句: 说明:sql的分页语句格式为select * from aaa limit #{startIndex},#{pageSize} //---------- ...
- 7_2 最大乘积(UVa11059)<枚举连续子序列>
给一个数字集合{ S1,S2,…,Sn },请从这个数字集合里找出一段连续数字,使他们的乘积是最大的.以Case 1为例子,2 x 4 = 8为这个集合的最大乘积:而Case 2则为2 x 5 x(– ...
- UNICODE编码UTF-16 中的Endian(FE FF) 和 Little Endian(FF FE)
从网上找到的两篇不错的文章,由于被网上多处转载,所以不知道源处,未能注明出处,希望作者见谅,如有意见请发信给我,谢谢! 第一篇很清晰. 介绍Unicode之前,首先要讲解一些基础知识.虽然跟Unico ...
- opencv:霍夫直线检测
#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...
- 1012 The Best Rank
1012 The Best Rank 1. 注意点 一名同学同样排名下的科目优先级问题 不同同学分数相同时排名相同,注意 排名不是 1 1 2 3 4 这种, 而是 1 1 3 4 5 注意到有些同学 ...