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 mvc 生成条形码
using System; using System.Collections; using System.Collections.Generic; using System.Drawing; usin ...
- Linux 重定向
Linux 标准文件描述符 描述符 缩写 描述 0 STDIN 标准输入 1 STDOUT 标准输出 2 STDERR 标准错误 3-9 应该是扩展的标准输出(待验证) 命令行重定 ...
- Oracle的关于建表,约束,查询等的练习
从建立一个简单表,到实现一些复杂查询的例子, DROP TABLE grade;DROP TABLE item;DROP TABLE sporter;CREATE TABLE sporter( spo ...
- C#读取txt文件返回DATATABLE
//1.打开资源管理器 OpenFileDialog open = new OpenFileDialog(); if (open.ShowDialog() == DialogResult.OK) { ...
- spark on yarn :state: ACCEPTED一直 出现
今天运行spark on yarn 一直出现 16/09/20 18:40:41 INFO yarn.Client: Application report for application_147417 ...
- 【linux信号】10.11信号集
POSIX定义数据类型sigset_t以包含一个信号集,并且定义了下面五个函数处理信号集:
- HTTP生命周期
HTTP生命周期 Http 请求 AspNet_ISAIP.DLL (ISAPI扩展,独立于站点外,用于可扩展的桥梁), w3wp.exe (net工作进程) IIS6 以上,6以下为aspnet_w ...
- linux查看网卡个数及速度
# lspci | grep Ethernet03:00.0 Ethernet controller: Broadcom Corporation NetXtreme II BCM5708 Gigabi ...
- js自定义类和对象及继承
1.工厂方式 <script type="text/javascript"> function createObject(name){ var p = new Obje ...
- nodejs学习笔记-1
nodejs入门-安装 nodejs是什么,刚接触了一段时间,我自己也说不清楚它.按我个人的简单理解,nodejs就是一个javascript的解析器,它让javascript不在局限于浏览器客户端. ...