uva 10635 LCS转LIS
这道题两个数组都没有重复的数字,用lcs的nlogn再适合不过了
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <cctype>
#include <vector>
#include <iterator>
#include <set>
#include <map>
#include <sstream>
using namespace std; #define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define pb push_back
#define debug printf("!\n")
#define MAXN 1005
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pqueue priority_queue
#define INF 0x3f3f3f3f int n,m,v; int q,p,top; int a[*],b[*],c[*],pos[*],stk[*]; void lis(int len)
{
mem(stk,);
top = ;
stk[] = -;
for(int i=;i<=len;i++)
{
if(c[i]>stk[top])
{
stk[++top] = c[i];
}
else
{
int lo=,hi=top,mid;
while(lo<=hi)
{
mid = (lo+hi)>>;
if(c[i]>stk[mid]) lo = mid+;
else hi = mid -;
}
stk[lo] = c[i];
}
}
} int main()
{
int i,j;
int t,kase=;
sf("%d",&t);
while(t--)
{
sf("%d%d%d",&n,&p,&q);
mem(pos,);
mem(c,);
v=;
for(i=;i<=p+;i++)
{
sf("%d",&a[i]);
pos[a[i]] = i;
}
for(i=;i<=q+;i++)
{
sf("%d",&b[i]);
if(pos[b[i]]) c[v++] = pos[b[i]];
}
lis(v);
pf("Case %d: %d\n",kase++,top);
}
}
uva 10635 LCS转LIS的更多相关文章
- O(nlogn)实现LCS与LIS
序: LIS与LCS分别是求一个序列的最长不下降序列序列与两个序列的最长公共子序列. 朴素法都可以以O(n^2)实现. LCS借助LIS实现O(nlogn)的复杂度,而LIS则是通过二分搜索将复杂度从 ...
- 最长公共子序列-LCS问题 (LCS与LIS在特殊条件下的转换) [洛谷1439]
题目描述 给出1-n的两个排列P1和P2,求它们的最长公共子序列. 输入 第一行是一个数n, 接下来两行,每行为n个数,为自然数1-n的一个排列. 输出 一个数,即最长公共子序列的长度 输入样例 5 ...
- BZOJ4990 (LCS转LIS)
题面 https://www.lydsy.com/JudgeOnline/problem.php?id=4990 分析 首先可以看出一个简单的DP dp[i][j]表示序列a前i个与序列b前j个连线数 ...
- UVA 10635 Prince and Princess【LCS 问题转换为 LIS】
题目链接: http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=19051 题意: 有两个长度分别为p+1和q+1的由1到n2 ...
- UVA 10635 - Prince and Princess LCS转化为LIS
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- Uva 10635 - Prince and Princess LCS/LIS
两个长度分别为p+1和q+1的由1到n2之前的整数组成的序列,每个序列的元素各不相等,两个序列第一个元素均为1.求两个序列的最长公共子序列 https://uva.onlinejudge.org/in ...
- uva 10635 Prince and Princess(LCS成问题LIS问题O(nlogn))
标题效果:有两个长度p+1和q+1该序列.的各种元素的每个序列不是相互同.并1~n^2之间的整数.个序列的第一个元素均为1. 求出A和B的最长公共子序列长度. 分析:本题是LCS问题,可是p*q< ...
- Uva 10635 - Prince and Princess 问题转化,元素互不相同(在自身序列中独特)的两个数列的LCS,LIS 难度: 2
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- UVA - 10635 LIS LCS转换
白书例题,元素互不相同通过哈希转换为LIS求LCS #include<iostream> #include<algorithm> #include<cstdio> ...
随机推荐
- 【ARC080F】Prime Flip 差分+二分图匹配
Description 有无穷个硬币,初始有n个正面向上,其余均正面向下. 你每次可以选择一个奇质数p,并将连续p个硬币都翻转. 问最小操作次数使得所有硬币均正面向下. Input 第一行 ...
- 1. Shell编程第一讲
(1)shell 历史: Shell的作用是解释执行用户的命令,用户输入一条命令,Shell就解释执行一条,这种方式称为交互式(Interactive). Shell还有一种执行命令的方式称为批处理( ...
- Js屏蔽键盘输入的某些字符,用以部分代替正则表达式
工作当中用到的:有是,用户会在文本框里输入一些无效的(错误的)内容,比如在手机号中输入#等等,一般使用正则表达式,但是只有点击的时候才会验证,用户体验不好,所以想屏蔽这些按键,让键盘根本打不出来,以下 ...
- docker 安装 redis
docker拉去镜像以及配置生成容器的步骤几乎和之前的nginx安装一样,直接写下面的命令了 1. docker pull redis 2. docker run -p 6379:6379 -v /U ...
- Qt 学习之路 2(76):QML 和 QtQuick 2
Home / Qt 学习之路 2 / Qt 学习之路 2(76):QML 和 QtQuick 2 Qt 学习之路 2(76):QML 和 QtQuick 2 豆子 2013年12月18日 Qt ...
- SprimgMVC学习笔记(十一)—— 解决静态资源无法被springmvc处理
方法一:在springmvc.xml中配置 <!-- 解决静态资源无法被springMVC处理的问题 --> <mvc:default-servlet-handler /> 方 ...
- 【记录】drozer与adb工具的安装与使用
drozer:链接:https://pan.baidu.com/s/1o8QOIF4 密码:a7yv adb:链接:https://pan.baidu.com/s/1o865VSm 密码:zq9t d ...
- <a>标签深入讲解
标签中 href 和 onclick 的区别,以及href="javascript:xxx(this);"与onclick="xxx(this);" 传递thi ...
- strus2配置strus.xml问题-The content of element type "package" must match "(result-types?,interceptors?
搭建strus2项目,在配置strus.xml时候碰到了这个问题: The content of element type "package" must match "( ...
- HTML盒子尺寸的计算
参考链接http://edu.51cto.com/lesson/id-54739.html