题目链接

给出两个序列, 求出他们的最长公共上升子序列。

两层循环, 内层循环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)算法的更多相关文章

  1. Codeforces Beta Round #7 D. Palindrome Degree manacher算法+dp

    题目链接: http://codeforces.com/problemset/problem/7/D D. Palindrome Degree time limit per test1 secondm ...

  2. Codeforces 845C. Two TVs 思路:简单贪心算法

    题目: 题目原文链接:http://codeforces.com/contest/845/problem/C 题意:现在我们有一个电视清单,有两个电视,电视清单上有每一个节目的开始时间和结束时间. 电 ...

  3. CodeForces 702 A Maximum Increase (贪心,高效算法)

    题意:给定 n 个数,问你连续的最长的序列是几个. 析:从头扫一遍即可. 代码如下: #include <cstdio> #include <string> #include ...

  4. NLP 第10章 基于深度学习的NLP 算法

  5. CodeForces - 617E XOR and Favorite Number 莫队算法

    https://vjudge.net/problem/CodeForces-617E 题意,给你n个数ax,m个询问Ly,Ry,  问LR内有几对i,j,使得ai^...^ aj =k. 题解:第一道 ...

  6. [10] 圆管(Pipe)图形的生成算法

    顶点数据的生成 bool YfBuildPipeVertices ( Yreal radius, Yreal assistRadius, Yreal height, Yuint slices, YeO ...

  7. 10^9以上素数判定,Miller_Rabin算法

    #include<iostream> #include<cstdio> #include<ctime> #include<string.h> #incl ...

  8. 算法专题 | 10行代码实现的最短路算法——Bellman-ford与SPFA

    今天是算法数据结构专题的第33篇文章,我们一起来聊聊最短路问题. 最短路问题也属于图论算法之一,解决的是在一张有向图当中点与点之间的最短距离问题.最短路算法有很多,比较常用的有bellman-ford ...

  9. Codeforces 193E - Fibonacci Number(打表找规律+乱搞)

    Codeforces 题目传送门 & 洛谷题目传送门 蠢蠢的我竟然第一眼想套通项公式?然鹅显然 \(5\) 在 \(\bmod 10^{13}\) 意义下并没有二次剩余--我真是活回去了... ...

随机推荐

  1. asp.net 页面跳转的方法

    目前知道有4种: 1超链接 2.response.redirect("urlString") 3.server.transfer("urlString") 4. ...

  2. jQuery的实用小技巧

    1. 禁止右键点击 $(function(){ $(document).bind('contextmenu', function(e){ return false; }) }) 2. 隐藏搜索文本框文 ...

  3. console.log几个小知识

    <script> //百度的console console.log('一张网页,要经历怎样的过程,才能抵达用户面前?\n一位新人,要经历怎样的成长,才能站在技术之巅?\n探寻这里的秘密:\ ...

  4. javascript定时器(上)

    (一).setInterval 间隔性 function show(){ alert(‘a’); } setInterval(show,1000); 每隔1000毫秒(1秒)执行一次show这个函数: ...

  5. Asp.net Mvc 中的模型绑定

    asp.net mvc中的模型绑定可以在提交http请求的时候,进行数据的映射. 1.没有模型绑定的时候 public ActionResult Example0() { ) { string id ...

  6. Log4j的应用实例(转)

    转自:http://www.cnblogs.com/eflylab/archive/2007/01/12/618080.html 在Log4J使用笔记中没有怎么写实例,那么在这篇中我将Log4j的一个 ...

  7. jQuery 中使用 JSON

    转载:http://www.cnblogs.com/haogj/archive/2011/12/01/2271098.html JSON 格式 json 是 Ajax 中使用频率最高的数据格式,在浏览 ...

  8. ubuntu常用命令(转)

    1.打开终端的方法 Ubuntu 中按左侧栏的第一个“面板主页(Dash 主页)”(可以按win键调出),在里面输入terminal可以打开终端,另外打开终端的快捷键是Ctrl+Alt+T 2.修改用 ...

  9. logstash 操作redis

    在实际应用中,Logstash进程会被氛围两个不同的角色. 运行在应用服务器上的尽量减轻运行压力,只做读取和转发,这个角色叫做shipper 运行在独立的服务器上完成数据解析处理,负责写入到Elast ...

  10. 利用 Windows Azure 实现“云优先”

    根据 IDC 的调查,云计算无疑为我们的合作伙伴提供了巨大的机会,预计到 2016 年,全球企业将在公共云服务上耗资 980 亿美元.在今天的休斯敦全球合作伙伴大会上,我们非常高兴能与合作伙伴共同寻求 ...