Codeforces Beta Round #10 D. LCIS
题目链接:
http://www.codeforces.com/contest/10/problem/D
D. LCIS
time limit per test:1 secondmemory limit per test:256 megabytes
问题描述
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.
输入
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.
输出
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.
样例
input
7
2 3 1 6 5 4 6
4
1 3 5 6output
3
3 5 6
题意
求最长公共递增子序列,并输出一个最优解。
题解
dp[j]表示第一个串的前i个和第二个串的前j个的以b[j]结尾的公共最长上升子序列的长度。
代码
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 555;
int dp[maxn];
int a[maxn], b[maxn];
int pre[maxn];
int n, m;
void print(int i) {
if (!i) return;
print(pre[i]);
printf("%d ", b[i]);
}
void init() {
memset(dp, 0, sizeof(dp));
memset(pre, 0, sizeof(pre));
}
int main() {
init();
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]);
int ans = 0, st = 0;
for (int i = 1; i <= n; i++) {
int pos = 0;
for (int j = 1; j <= m; j++) {
if (b[j]<a[i] && dp[pos]<dp[j]) {
pos = j;
}
else if (a[i] == b[j]) {
dp[j] = dp[pos] + 1;
pre[j] = pos;
}
//这样边扫边记会wa,还没找到原因。。
//if (ans<dp[j]) {
// ans = dp[j], st = j;
//}
}
}
for (int i = 1; i <= m; i++) {
if (dp[i] > ans) {
ans = dp[i], st = i;
}
}
printf("%d\n", ans);
print(st);
return 0;
}
再一发:
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=555;
const int maxm=555;
int a[maxn],b[maxn];
int dp[maxn][maxn],pre[maxn][maxn];
int main() {
int n;
scf("%d",&n);
for(int i=1;i<=n;i++) scf("%d",&a[i]);
int m;
scf("%d",&m);
for(int i=1;i<=m;i++) scf("%d",&b[i]);
clr(dp,0);
clr(pre,0);
for(int i=1;i<=n;i++){
int ma=0,p=0;
for(int j=1;j<=m;j++){
dp[i][j]=dp[i-1][j];
pre[i][j]=pre[i-1][j];
if(a[i]==b[j]){
if(dp[i][j]<ma+1){
dp[i][j]=ma+1;
pre[i][j]=p;
}
}else if(b[j]<a[i]){
if(ma<dp[i-1][j]){
ma=dp[i-1][j];
p=j;
}
}
}
}
int ans=0,pos=0;
for(int i=1;i<=m;i++){
if(ans<dp[n][i]){
ans=dp[n][i];
pos=i;
}
}
VI lis;
lis.pb(pos);
int p=pre[n][pos];
while(p){
lis.pb(p);
p=pre[n][p];
}
reverse(lis.begin(),lis.end());
printf("%d\n",ans);
if(ans==0) return 0;
rep(i,0,lis.sz()-1) prf("%d ",b[lis[i]]);
prf("%d\n",b[lis[lis.sz()-1]]);
return 0;
}
//end-----------------------------------------------------------------------
/*
3 1 2 3
3 3 4 5
*/
Codeforces Beta Round #10 D. LCIS的更多相关文章
- 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 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: ...
随机推荐
- 熔断器设计模式<转>
熔断器设计模式 如果大家有印象的话,尤其是夏天,如果家里用电负载过大,比如开了很多家用电器,就会”自动跳闸”,此时电路就会断开.在以前更古老的一种方式是”保险丝”,当负载过大,或者电路发生故障或异常时 ...
- html5的自定义data-*属性和jquery的data()方法的使用示例
人们总喜欢往HTML标签上添加自定义属性来存储和操作数据. 但这样做的问题是,你不知道将来会不会有其它脚本把你的自定义属性给重置掉,此外,你这样做也会导致html语法上不符合Html规范,以及一些其它 ...
- sql server 修改列类型
如下代码中为修改bcp数据库中表B_TaskFileMonitor中的列FileSizeOriginal的类型为bigint use bcp; ); --判断是否存在这一列 IF COL_LENGTH ...
- Facebook抛弃了HTML5,微信却捧火了它
苹果普及了HTML5技术,Facebook押注HTML5上,却受到不小的打击,导致在后来一段时间里,唱衰HTML5的言论成为媒体的一种幸灾乐祸的态度,人人避而不谈.微信通过公众号的形式,以游戏.营销重 ...
- 2015年最新出炉的JavaScript开发框架
前端框架简化了开发过程中,像 Bootstrap 和 Foundation 就是前端框架的佼佼者.在这篇文章了,我们编制了一组新鲜的,实用的,可以帮助您建立高质量的 Web 应用程序的 JavaScr ...
- MyBatis用嵌套ResultMap实现一对多映射
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3959451.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...
- 使用struct实现面向对象编程的封装
虽然C是面向过程的语言,但是这不代表C不能使用面向对象的思想,本质上说语言只是一种手段而已,一种外在的表现形式,支持面向对象的语言只是通过设计的特定的关键字更好的表现了面向对象编程而已.C中也可以使用 ...
- Android开发代码规范
目录 1.命名基本原则 2.命名基本规范 2.1编程基本命名规范 2.2分类命名规范 3.分类命名规范 3.1基本数据类型命名规范 3.2控件命名规范 3.3变量命名规范 3.4整个项目的目录规范化 ...
- C# 玩家昵称屏蔽敏感字眼
功能:使用正则 对玩家昵称处理,如果含有 屏蔽字库里的敏感字眼进行屏蔽.已封装成dll 1.屏蔽字库处理成所需要的正则格式:(所需正则表达式格式:".*((XX)|(XX)|(XX)|.. ...
- windowsphone 瀑布流&ui虚拟化
瀑布流已经有点年代了吧,不过wp上还真是挺少资料的.今天抽空把自己之前搞过的东西写出来,避免大家重复劳动. 一.简单的瀑布流排版加入ui虚拟化. 最近看了 段博琼 ui虚拟化的一篇博文,链接:htt ...