Codeforces Beta Round #10 D. LCIS(DP&LCIS)
1 second
256 megabytes
standard input
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 aby
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 (1 ≤ n ≤ 500)
— the length of the first sequence. The second line contains nspace-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.
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.
7
2 3 1 6 5 4 6
4
1 3 5 6
3
3 5 6
5
1 2 0 2 1
3
1 0 1
2
0 1
题意:
给你长度分别为n,m(1<=n,m<=500)的序列。要你求这两个序列的最长公共上升子序列。
思路:
最长公共子序列做过。最长上升子序列也做过。可是这题时最长公共上升子序列。。。解法肯定还是dp拉。
感觉这题的dp方程的思想非常不错,体现了一种加强约束的思想。dp[i][j]表示。处理完A序列的前i个,且上升序列以B序列的B[j]结尾的最长子序列。感觉这个把状态体现以什么结尾是非常不错的思想。然后转移显而易见了。
if(A[i]==B[j])
dp[i][j]=dp[i-1][k];//k是小于j且B[k]<B[j]
else
dp[i][j]=dp[i-1][j];
我们能够i,j循环这样就能够省掉找k的时间。复杂度O(n*m)
具体见代码:
#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=100010;
typedef long long ll;
int dp[550][550],A[550],B[550],path[550][550],n,m;
bool print(int x)
{
if(!x)
return false;
if(print(path[n][x]))
printf(" %d",B[x]);
else
printf("%d",B[x]);
return true;
}
int main()
{
int i,j,tp,ans,pos;
while(~scanf("%d",&n))
{
for(i=1;i<=n;i++)
scanf("%d",&A[i]);
scanf("%d",&m);
for(i=1;i<=m;i++)
scanf("%d",&B[i]);
memset(dp,0,sizeof dp);
for(i=1;i<=n;i++)
{
tp=pos=0;
for(j=1;j<=m;j++)
{
dp[i][j]=dp[i-1][j];
path[i][j]=path[i-1][j];
if(A[i]==B[j]&&tp+1>dp[i][j])
dp[i][j]=tp+1,path[i][j]=pos;
if(B[j]<A[i]&&dp[i-1][j]>tp)
tp=dp[i-1][j],pos=j;
}
}
ans=1;
for(i=1;i<=m;i++)
if(dp[n][i]>dp[n][ans])
ans=i;
printf("%d\n",dp[n][ans]);
if(dp[n][ans])
print(ans);
printf("\n");
}
return 0;
}
版权声明:本文博主原创文章。博客,未经同意不得转载。
Codeforces Beta Round #10 D. LCIS(DP&LCIS)的更多相关文章
- Codeforces Beta Round #13 C. Sequence (DP)
题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...
- 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 动态规划
D. LCIS 题目连接: http://www.codeforces.com/contest/10/problem/D Description This problem differs from o ...
- 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 #17 C. Balance (字符串计数 dp)
C. Balance time limit per test 3 seconds memory limit per test 128 megabytes input standard input ou ...
- Codeforces Beta Round #71 C【KMP+DP】
Codeforces79C 题意: 求s串的最大子串不包含任意b串: 思路: dp[i]为以i为起点的子串的最长延长距离. 我们可以想到一种情况就是这个 i 是某个子串的起点,子串的长度-1就是最短, ...
- Codeforces Beta Round #96 (Div. 2) (A-E)
写份DIV2的完整题解 A 判断下HQ9有没有出现过 #include <iostream> #include<cstdio> #include<cstring> ...
随机推荐
- bzoj2011: [Ceoi2010]Mp3 Player
Description Georg有个MP3 Player,没有任何操作T秒钟就会锁定,这时按下任意一个键就会变回没锁定的状态,但不会改变频道.只有在没锁定的状态下按键才有可能改变频道. MP3的频道 ...
- Codeforces Round #Pi (Div. 2)
上次比完赛就准备写了, 结果懒癌发作了, 拖到了现在. Problem_A: 题意: 在一条x轴上有n座城市, 每个城市之间的距离就是它们对应坐标的距离, 现在求出每个城市到其他城市的最近距离和最远距 ...
- bzoj 1501: [NOI2005]智慧珠游戏 Dancing Link
1501: [NOI2005]智慧珠游戏 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 190 Solved: 122[Submit][Status] ...
- [BZOJ 3669] [Noi2014] 魔法森林 【LCT】
题目链接:BZOJ - 3669 题目分析 如果确定了带 x 只精灵A,那么我们就是要找一条 1 到 n 的路径,满足只经过 Ai <= x 的边,而且要使经过的边中最大的 Bi 尽量小. 其实 ...
- 如何与 DevOps 为伍?
DevOps 是一个席卷 IT 界的新术语.但它究竟是什么,南非的公司们如何利用它来加快高品质应用程序的开发速度?国外知名博客作者凯西·吉布森找到了一些答案. 其实 DevOps 这个词已经火了一段时 ...
- JavaService wrapper
http://my.oschina.net/yjwxh/blog/260835 http://blog.chinaunix.net/uid-664509-id-3398193.html http:// ...
- Ultraedit中使用Astyle格式化代码
方法: 使用UE的自定义工具栏并借助开源工具astyle.exe来完成. 1. 首先下载最新的astyle,因为ue自带的astyle版本太老,不支持空格.中文名等. http://astyle.so ...
- EditText的 焦点事件 setOnFocusChangeListener
实现代码: //光标处在EditText时其内容消失 mInfo = (EditText)findViewById(R.id.old_password); //setOnFocusChangeList ...
- java 集合接口及类
- SPOJ-COT-Count on a tree(树上路径第K小,可持久化线段树)
题意: 求树上A,B两点路径上第K小的数 分析: 同样是可持久化线段树,只是这一次我们用它来维护树上的信息. 我们之前已经知道,可持久化线段树实际上是维护的一个前缀和,而前缀和不一定要出现在一个线性表 ...