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}\) 意义下并没有二次剩余--我真是活回去了... ...
随机推荐
- JavaScript Dispatch Event
<html> <head> <script type="text/javascript"> function performClick(elem ...
- FPGrowth算法原理
算法实现: /** * FPGrowth算法的主要思想: * 1. 构造频繁1项集:遍历初始数据集构造频繁1项集,并作为项头表,建立将指向fpTree节点对应元素的引用 * 2. 构造FPTree:再 ...
- python基础之 re(正则表达式)模块学习
今天学习了Python中有关正则表达式的知识.关于正则表达式的语法,不作过多解释,网上有许多学习的资料.这里主要介绍Python中常用的正则表达式处理函数. re.match re.match 尝试从 ...
- c++线程创建传递的参数发生改变
看看如下代码,觉得输出会是什么? #include "stdafx.h" #include <windows.h> #include <iostream> ...
- ajax中返回包含html的奇葩问题
如果通过ajax返回一串包含有html标签的字符串,然后添加到相应的html文档位置,如果标签外含有空格或者换行等就会报错.
- Java split方法源码分析
Java split方法源码分析 public String[] split(CharSequence input [, int limit]) { int index = 0; // 指针 bool ...
- Java数据结构习题_算法分析
2.设T1(N)=O(f(N)),T2(N)=O(f(N)),则: T1(N)-T2(N)=o(f(N)) False,若1位2N,2为N T1(N)/T2(N)=O(1) ...
- IO-04. 混合类型数据格式化输入
/** *A4-IO-04. 混合类型数据格式化输入 *C语言实现 *测试已通过 */ #include "stdio.h" int main() { float m1,m2; i ...
- elasticsearch 索引 类型 id
zjtest7-frontend:/usr/local/logstash-2.3.4/config# cat logstash_indexer01.conf input { redi ...
- 最全面 Nginx 入门教程 + 常用配置解析
转自 http://blog.csdn.net/shootyou/article/details/6093562 Nginx介绍和安装 一个简单的配置文件 模块介绍 常用场景配置 进阶内容 参考资料 ...