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}\) 意义下并没有二次剩余--我真是活回去了... ...
随机推荐
- Http权威指南笔记(二) Http状态码大全
100~199—信息状态码 200~299—成功状态码 客户端发请求时,这些请求通常都是成功的. 300~399—重定向状态码 重定向状态码告知客户端使用代替位置来访问他们所感兴趣的资源. 400~4 ...
- jQuery 随滚动条滚动效果 (适用于内容页长文章)
直接入题! 当内容页比较长的时候,网站右侧一直是空白,不如放点有用的东西给用户看,最好不要放广告,因为那样很邪恶,你懂的. 好吧,昨天写了这个东西,jQuery滚动随动区块,代码如下: //侧栏随动 ...
- javascript获取元素结点到页面的绝对距离的方式
var div = document.getElementById('div');var p = getPos(div); function getPos(obj) { var pos = {left ...
- PHP扫雷(转载)。
<?php @$init = $_POST["init"];//game restart @$clickvalue = $_POST["clickvalue& ...
- jquery序列化元素
序列化元素: 做项目的时候,表单是必不可少的,经常用来提交数据,例如注册.登陆等.常规的方法是使表单提交到另外一个页面,整个浏览器都会被刷新,而使用Ajax技术则能够异步地提交表单,并将服务器返回的数 ...
- Python学习路径和个人增值(整合版)
PS:内容来源于网络 一.简介 Python是一种面向对象.直译式计算机程序设计语言,由Guido van Rossum于1989年底发明.由于他简单.易学.免费开源.可移植性.可扩展 ...
- VB2012读取xml
上回谢了生成写xml的,现在把读取的补上 文件如下 <?xml version="1.0" encoding="UTF-8" standalone=&qu ...
- eclipse android sdk javadoc
sdk 的函数不提示帮助信息 查了下是现在adt版本没有doc文件夹,拷贝了早期的版本docs过来 其他具体操作如下:http://blog.csdn.net/lyh7736362/article/d ...
- 5路数字量输入Di,5路大电流继电器输出,可电脑控制,支持modbus协议工业模块,支持和DCS,PLC无缝对接。
数字量输入输出模块MRD-5060具有5路DI,5路继电器输出,1路485接口(支持Modbus RTU),能实现5路DI(干接点输入)转485的采集,也可以通过485控制5路继电器(5A30VDC, ...
- 为Delphi程序增加UAC功能
相关资料:http://bbs.csdn.net/topics/320071356# 操作方法: 在Source\VCL目录下应该有这样两个文件sample.manifest和WindowsXP.rc ...