博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces 10D
阅读量:4627 次
发布时间:2019-06-09

本文共 2472 字,大约阅读时间需要 8 分钟。

D. LCIS
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

This problem differs from one which was on the online contest.

The sequence a1, a2, ..., an is called increasing, if ai < ai + 1 for i < n.

The sequence s1, s2, ..., sk is called the subsequence of the sequence a1, a2, ..., an, if there exist such a set of indexes 1 ≤ i1 < i2 < ... < ik ≤ n that aij = sj. In other words, the sequence s can be derived from the sequence 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.

Input

The first line contains an integer n (1 ≤ n ≤ 500) — the length of the first sequence. The second line contains n space-separated integers from the range [0, 109] — elements of the first sequence. The third line contains an integer m (1 ≤ m ≤ 500) — the length of the second sequence. The fourth line contains m space-separated integers from the range [0, 109] — elements of the second sequence.

Output

In the first line output 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.

Examples
input
7 2 3 1 6 5 4 6 4 1 3 5 6
output
3 3 5 6
input
5 1 2 0 2 1 3 1 0 1
output
2 0 1

题意:给出两个序列,求其最长上升子序列,并输出该序列中的数,该题含有Special Judge。

 

题解:DP。这应该是很经典的问题了,DP方程如下,这里不再赘述。

a[i]=b[j]时,f[j]=f[la]+1;

a[i]>b[j]时,若f[j]>f[la],则说明接下来从j位置开始比从la位开始优,所以用j替换la,即la=j。

路径记录的是b数组的下标,打印时递归输出。

注意一个坑点:当公共长度为0时,不需要输出任何路径。

1 #include
2 using namespace std; 3 const int N=505; 4 int n,m,x,ans,a[N],b[N],f[N],pre[N]; 5 void print(int x) 6 { 7 if (!x) return; 8 print(pre[x]); 9 printf("%d ",b[x]);10 }11 int main()12 {13 scanf("%d",&n);14 for (int i=1;i<=n;++i)15 scanf("%d",&a[i]);16 scanf("%d",&m);17 for (int i=1;i<=m;++i)18 scanf("%d",&b[i]);19 for (int i=1;i<=n;++i)20 {21 int la=0;22 for (int j=1;j<=m;++j)23 {24 if (a[i]==b[j]) f[j]=f[la]+1,pre[j]=la;25 else if (a[i]>b[j]&&f[j]>f[la]) la=j;26 }27 }28 for (int i=1;i<=m;++i)29 if (f[i]>ans) ans=f[i],x=i;30 printf("%d\n",ans);31 print(x);32 return 0;33 }
View Code

 

转载于:https://www.cnblogs.com/zk1431043937/p/7725668.html

你可能感兴趣的文章
面向对象(类的概念,属性,方法,属性的声明,面向对象编程思维
查看>>
2019.07.16
查看>>
ora-1031解决一例
查看>>
虚拟机oom
查看>>
Mysql 多表使用 Case when then 遇到的坑
查看>>
第八次作业
查看>>
天秤座的爱情(转)
查看>>
今天开始搞CentOS 7
查看>>
查询今天是周几?
查看>>
CSS中position属性( absolute | relative | static | fixed )详解
查看>>
(第四周)要开工了
查看>>
Python爬虫
查看>>
rsync工具
查看>>
博客园第一天,开放封闭原则
查看>>
【WA】九度OJ题目1435:迷瘴
查看>>
CSS 实例之打开大门
查看>>
BGA封装芯片手工焊接攻略
查看>>
抽象类和接口的联系与区别
查看>>
ie8的input的placeholder不显示的解决bug
查看>>
cmake, This may result in binaries being created in the wrong place
查看>>