codeforces 10 D. LCIS LCIS O(n^2)算法
给出两个序列, 求出他们的最长公共上升子序列。
两层循环, 内层循环j, 外层i。 如果a[i] == b[j], 那么dp[j] = max(dp[j], dp[best]+1), best是一个指针, 指向小于j的元素中dp值最大并且b[best]的值小于a[i]的元素。
如果a[i]>b[j], 那么就看dp[j]是否大于dp[best], 如果大于就更新best。
具体看代码。
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
int dp[], a[], b[], pre[], ans[];
int main()
{
int n, m;
cin.tie();
ios::sync_with_stdio();
cout.tie();
cin>>n;
for(int i = ; i<=n; i++)
cin>>a[i];
cin>>m;
for(int i = ; i<=m; i++)
cin>>b[i];
for(int i = ; i<=n; i++) {
int best = ;
for(int j = ; j<=m; j++) {
if(a[i] == b[j] && dp[j]<dp[best]+) {
dp[j] = dp[best]+;
pre[j] = best;
} else if(a[i]>b[j]&&dp[j]>dp[best]) {
best = j;
}
}
}
int maxx = , cnt = ;
for(int i = ; i<=m; i++)
if(dp[i]>dp[maxx])
maxx = i;
cout<<dp[maxx]<<endl;
for(int i = maxx; i!=; i = pre[i])
ans[cnt++] = b[i];
for(int i = cnt-; i>=; i--)
cout<<ans[i]<<" ";
return ;
}
codeforces 10 D. LCIS LCIS O(n^2)算法的更多相关文章
- Codeforces Beta Round #7 D. Palindrome Degree manacher算法+dp
题目链接: http://codeforces.com/problemset/problem/7/D D. Palindrome Degree time limit per test1 secondm ...
- Codeforces 845C. Two TVs 思路:简单贪心算法
题目: 题目原文链接:http://codeforces.com/contest/845/problem/C 题意:现在我们有一个电视清单,有两个电视,电视清单上有每一个节目的开始时间和结束时间. 电 ...
- CodeForces 702 A Maximum Increase (贪心,高效算法)
题意:给定 n 个数,问你连续的最长的序列是几个. 析:从头扫一遍即可. 代码如下: #include <cstdio> #include <string> #include ...
- NLP 第10章 基于深度学习的NLP 算法
- CodeForces - 617E XOR and Favorite Number 莫队算法
https://vjudge.net/problem/CodeForces-617E 题意,给你n个数ax,m个询问Ly,Ry, 问LR内有几对i,j,使得ai^...^ aj =k. 题解:第一道 ...
- [10] 圆管(Pipe)图形的生成算法
顶点数据的生成 bool YfBuildPipeVertices ( Yreal radius, Yreal assistRadius, Yreal height, Yuint slices, YeO ...
- 10^9以上素数判定,Miller_Rabin算法
#include<iostream> #include<cstdio> #include<ctime> #include<string.h> #incl ...
- 算法专题 | 10行代码实现的最短路算法——Bellman-ford与SPFA
今天是算法数据结构专题的第33篇文章,我们一起来聊聊最短路问题. 最短路问题也属于图论算法之一,解决的是在一张有向图当中点与点之间的最短距离问题.最短路算法有很多,比较常用的有bellman-ford ...
- Codeforces 193E - Fibonacci Number(打表找规律+乱搞)
Codeforces 题目传送门 & 洛谷题目传送门 蠢蠢的我竟然第一眼想套通项公式?然鹅显然 \(5\) 在 \(\bmod 10^{13}\) 意义下并没有二次剩余--我真是活回去了... ...
随机推荐
- asp.net 两个页面之前传递数据
.在两个表单之间传递数据 看下面的代码: 对于WebForm1: private void Page_Load(object sender, System.EventArgs e) { ArrayLi ...
- C# 8 函数 调用 常用类 时间 日期型
函数:能够独立完成某个功能的模块. 好处:1.结构更清析(编写.维护方便 ).2.代码重用.3.分工开发. 四要素:名称,输入(参数),输出(返回的类型),加工(函数体) 语法: 返回类型 函数名(参 ...
- Python基础之 urllib模块urlopen()与urlretrieve()的使用方法详解。
Python urllib模块urlopen()与urlretrieve()的使用方法详解 1.urlopen()方法urllib.urlopen(url[, data[, proxies]]) ...
- python基础教程第2章——列表与元组笔记
1.序列是Python中最基本的数据结构.序列中的每个元素被分配一个序列号——元素的位置,也称索引,第1个索引是0,第2为1,以此类推.序列中的最后1个元素为-1,倒数第2个位-2. python中有 ...
- block,inline,inline-block
block元素的特点是: 总是在新行上开始: 高度,行高以及顶和底边距都可控制: 宽度缺省是它的容器的100%,除非设定一个宽度 <div>, <p>, <h1>, ...
- 汽车总线obd模拟器,obd仿真器,ecu模拟器,obd开发测试工具,可以模拟ecu中的obd协议 MRD-5050
汽车总线OBD模拟器MRD-5050型号是在车辆越来越趋于网络化的趋势下研发的,是汽车产品开发.调试.生产必备的工具,能为为开发人员节省大量的时间.当前车辆上的总线设备越来越多,有的高端车上甚至多到有 ...
- 好的组件,无须太复杂 – KISSY Slide 组件简介
KISSY Slide 组件首页:http://gallery.kissyui.com/slide/1.1/guide/index.html V1.1 New Featurs Slide是一个幻灯切换 ...
- RHEL安装时加载第三方raid驱动
IBM x3650 M3服务器做完RAID之后,不能直接的安装Linux系统,会报出没有硬盘的错误 过程如下: 1.到IBM的官方网站下载device drive 下载后的为:ibm_dd_sraid ...
- egret随笔-publish命令的改进
缘由 导了几天的ipa,每次publish后都要改zip包名的代码,终于鼓起勇气翻看了一下egret publish的代码,唉,这代码...应该不会是北京的那几个大牛写的吧??? 正题 看了源码才知道 ...
- 数据库设计与SQL优化的建议
1. 用程序中,保证在实现功能的基础上,尽量减少对数据库的访问次数:通过搜索参数,尽量减少对表的访问行数,最小化结果集,从而减轻网络负担:能够分开的操作尽量分开处理,提高每次的响应速度:在数据窗口使用 ...