Codeforces Beta Round #10 D. LCIS 动态规划
D. LCIS
题目连接:
http://www.codeforces.com/contest/10/problem/D
Description
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.
Sample Input
7
2 3 1 6 5 4 6
4
1 3 5 6
Sample Output
3
3 5 6
Hint
题意
给你两个串,求公共最长上升子序列
题解:
考虑暴力枚举第一个串,然后for循环枚举第二个串
dp[i]表示第二个串位置为i的时候与第一个串的的最长公共上升子序列是多少
然后直接暴力更新dp就好了
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 505;
int dp[maxn],a[maxn],b[maxn],step[maxn],n,m;
void print(int x)
{
if(x==0)return;
print(step[x]);
printf("%d ",b[x]);
}
int main()
{
scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d",&a[i]);
scanf("%d",&m);for(int i=1;i<=m;i++)scanf("%d",&b[i]);
for(int i=1;i<=n;i++)
{
int pos = 0;
for(int j=1;j<=m;j++)
{
if(a[i]==b[j])
{
dp[j]=dp[pos]+1;
step[j]=pos;
}
else if(a[i]>b[j]&&dp[pos]<dp[j])
pos=j;
}
}
int ans = 0,ans1 = 0;
for(int i=1;i<=m;i++)
if(dp[i]>ans1)
ans1=dp[i],ans=i;
cout<<ans1<<endl;
print(ans);
}
Codeforces Beta Round #10 D. LCIS 动态规划的更多相关文章
- Codeforces Beta Round #10 D. LCIS
题目链接: http://www.codeforces.com/contest/10/problem/D D. LCIS time limit per test:1 secondmemory limi ...
- Codeforces Beta Round #10 D. LCIS(DP&LCIS)
D. LCIS time limit per test 1 second memory limit per test 256 megabytes input standard input output ...
- Codeforces Beta Round #10 C. Digital Root 数学
C. Digital Root 题目连接: http://www.codeforces.com/contest/10/problem/C Description Not long ago Billy ...
- Codeforces Beta Round #10 B. Cinema Cashier 暴力
B. Cinema Cashier 题目连接: http://www.codeforces.com/contest/10/problem/B Description All cinema halls ...
- Codeforces Beta Round #10 A. Power Consumption Calculation 水题
A. Power Consumption Calculation 题目连接: http://www.codeforces.com/contest/10/problem/A Description To ...
- Codeforces Beta Round #10 B. Cinema Cashier (树状数组)
题目大意: n波人去k*k的电影院看电影. 要尽量往中间坐,往前坐. 直接枚举,贪心,能坐就坐,坐在离中心近期的地方. #include <cstdio> #include <ios ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #13 C. Sequence (DP)
题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...
- Codeforces Beta Round 84 (Div. 2 Only)
layout: post title: Codeforces Beta Round 84 (Div. 2 Only) author: "luowentaoaa" catalog: ...
随机推荐
- (3)剑指Offer之数值的整数次方和调整数组元素顺序
一 数值的整数次方 题目描述: 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 问题解析: 这道题算是比较麻烦和难一点的一个了.我这里采 ...
- python基础===100盏灯的问题
闪存里有人这样提问这样: 第一轮操作所有电灯,第二轮操作第2盏,第4盏开关,以此类推,第三轮改变编号为3的倍数的电灯,第3盏,第6盏,如果原来那盏灯是亮的,就熄灭它,如果原来是灭的,就点亮它,以此类推 ...
- Deep Learning基础--随时间反向传播 (BackPropagation Through Time,BPTT)推导
1. 随时间反向传播BPTT(BackPropagation Through Time, BPTT) RNN(循环神经网络)是一种具有长时记忆能力的神经网络模型,被广泛用于序列标注问题.一个典型的RN ...
- Windows下Oracle数据库自动备份批处理脚本
expdb命令版本 @echo off REM ########################################################### REM # Windows Se ...
- java你应该学会什么
给初学者之一:浅谈java及应用学java 先说什么是Javajava是一种面向对象语言,真正的面向对象,任何函数和变量都以类(class)封装起来至于什么是对象什么是类,我就不废话了关于这两个概念的 ...
- 开源介绍:Google Guava、Google Guice、Joda-Time
一.Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concurrency lib ...
- SSD回归类物体检测
本宝宝最近心情不会,反正这篇也是搬用别人博客的了:(SSD就是YOLO+anchor(不同feature map 作为input)) 引言 这篇文章是在YOLO[1]之后的一篇文章,这篇文章目前是一篇 ...
- [你必须知道的.NET]第二十四回:认识元数据和IL(上)
发布日期:2009.02.24 作者:Anytao © 2009 Anytao.com ,Anytao原创作品,转贴请注明作者和出处. 说在,开篇之前 很早就有说说Metadata(元数据)和IL(中 ...
- js学习笔记1:语法、数据类型与转换、运算符与运算
注意: 上部代码错误,将停止运行,下部的代码无法显示 typeof 用来定义内容类型,不会输出内容只会输出类型 一.js输出语法 1. 弹窗输出('')内的内容: ...
- [实战]MVC5+EF6+MySql企业网盘实战(5)——登录界面,头像等比例压缩
写在前面 关于该项目,已经很久没更新了.实在是找不到一个好的ui,没办法就在网上找了一个还不错的,就凑合着先用着吧,先出第一版,以后的再想着去优化.最近更新与网盘项目相关的内容是准备在项目中使用一个美 ...