uva10635 LIS
Prince and Princess
Input: Standard Input
Output: Standard Output
Time Limit: 3 Seconds
In an n x n chessboard, Prince and Princess plays a game. The squares in the chessboard are numbered 1, 2, 3 ... n*n, as shown below:
Prince stands in square 1, make p jumps and finally reach square n*n. He enters a square at most once. So if we use xp to denote the p-th square he enters, then x1, x2, ... xp+1 are all different. Note that x1 = 1 and xp+1 = n*n. Princess does the similar thing - stands in square 1, make q jumps and finally reach square n*n. We use y1, y2 , ... yq+1 to denote the sequence, and all q+1 numbers are different.
Figure 2 belows show a 3x3 square, a possible route for Prince and a different route for Princess.
The Prince moves along the sequence: 1 --> 7 --> 5 --> 4 --> 8 --> 3 --> 9 (Black arrows), while the Princess moves along this sequence: 1 --> 4 --> 3 --> 5 --> 6 --> 2 --> 8 --> 9 (White arrow).
The King -- their father, has just come. "Why move separately? You are brother and sister!" said the King, "Ignore some jumps and make sure that you're always together."
For example, if the Prince ignores his 2nd, 3rd, 6th jump, he'll follow the route: 1 --> 4 --> 8 --> 9. If the Princess ignores her 3rd, 4th, 5th, 6th jump, she'll follow the same route: 1 --> 4 --> 8 --> 9, (The common route is shown in figure 3) thus satisfies the King, shown above. The King wants to know the longest route they can move together, could you tell him?
Input
Output
For each test case, print the case number and the length of longest route. Look at the output for sample input for details.
Sample Input Output for Sample Input
|
1 3 6 7 1 7 5 4 8 3 9 1 4 3 5 6 2 8 9 |
Case 1: 4 |
Problemsetter: Man Rujia Liu Man, Member of Elite Problemsetters' Panel
Pictures drawn by Shahriar Manzoor, Member of Elite Problemsetters' Panel
题意:
有2个长度为p+1和q+1的序列,每个序列中的值都是唯一的,求2个序列的最长公共子序列。
思路:
如果做lcs复杂度为n^2,超时。我们队A数组内的元素重新编号,如果B数组中的元素之前在A中没有编号,那么说明这个元素不在A中出现,可以在B中删去
该元素。B元素中的值都有一个编号,并且该编号是根据A数组内元素的位置来给定的,所以现在只要求B数组的LIS。(B中有编号的A中一定出现,并且B中的值表示
这个数在A数组中的位置)
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<time.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1000000000
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define key_value ch[ch[root][1]][0]
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = ;
int num[MAXN],b[MAXN],n,q,p;
int g[MAXN];
int main(){
int t,Case = ;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&q,&p);
memset(num,,sizeof(num));
for(int i = ; i <= q+; i++){
int x;
scanf("%d",&x);
num[x] = i;
}
int cnt = ;
for(int i = ; i <= p+; i++){
int x;
scanf("%d",&x);
if(num[x])b[cnt++] = num[x];
}
int ans = ;
g[] = ;
for(int i = ; i <= cnt; i++)g[i] = INF;
for(int i = ; i < cnt; i++){
int tp = lower_bound(g+,g+cnt+,b[i]) - g;
ans = max(ans,tp);
g[tp] = min(g[tp],b[i]);
}
printf("Case %d: %d\n",++Case,ans);
}
return ;
}
uva10635 LIS的更多相关文章
- UVa10635 - Prince and Princess(LCS转LIS)
题目大意 有两个长度分别为p+1和q+1的序列,每个序列中的各个元素互不相同,且都是1~n^2之间的整数.两个序列的第一个元素均为1.求出A和B的最长公共子序列长度. 题解 这个是大白书上的例题,不过 ...
- uva10635 LCS映射转LIS
题目给定 2个序列,要我们求LCS,但是序列的长度最长是250*250, LCS的时间复杂度是O(N*N),所以无法解决 我们可以第一个序列的数字,按位置,映射为1.2.3.4.5.6.7.8.9 那 ...
- [noip科普]关于LIS和一类可以用树状数组优化的DP
预备知识 DP(Dynamic Programming):一种以无后效性的状态转移为基础的算法,我们可以将其不严谨地先理解为递推.例如斐波那契数列的递推求法可以不严谨地认为是DP.当然DP的状态也可以 ...
- nlogn LIS模板
nlogn 模板 最长上升 #include<bits/stdc++.h> using namespace std; ; int n,x,y,a[N],num[N],d[N],len; / ...
- UVa 10635 王子和公主(LCS转LIS)
https://vjudge.net/problem/UVA-10635 题意: 有两个长度分别为p+1和q+1的序列,每个序列中的各个元素互不相同,且都是1~n^2之间的整数.两个序列的第一个元素均 ...
- 【转】关于LIS和一类可以用树状数组优化的DP 预备知识
原文链接 http://www.cnblogs.com/liu-runda/p/6193690.html 预备知识 DP(Dynamic Programming):一种以无后效性的状态转移为基础的算法 ...
- Lis日常维护
1.[问题]护士站打印LIs条码,出来是PDF格式的 [解决]在文件夹Client\NeusoftLis\Xml\Print.xml中把BarcodePrint Name的值改成安装的斑马打印机名(不 ...
- Codeforces 486E LIS of Sequence 题解
题目大意: 一个序列,问其中每一个元素是否为所有最长上升子序列中的元素或是几个但不是所有最长上升子序列中的元素或一个最长上升子序列都不是. 思路: 求以每一个元素为开头和结尾的最长上升子序列长度,若两 ...
- 出操队形(LIS)
题目来源:微策略2013年校园招聘面试一面试题 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往楼下跑了,然后身高矮的排在队伍的前面,身高较 ...
随机推荐
- 最大半连通子图 bzoj 1093
最大半连通子图 (1.5s 128MB) semi [问题描述] 一个有向图G = (V,E)称为半连通的(Semi-Connected),如果满足:∀ u, v ∈V,满足u->v 或 v - ...
- OS存储管理——FIFO,LRU,OPT命中率
课程设计课题 存储管理程序设计 摘 要 虚拟存储器作为现代操作系统中存储管理的一项重要技术,实现了内存扩充功能.而分页请求分页系统正好可以完美的支持虚拟存储器功能,它具有请求调页功能和页面置换功能.在 ...
- 用Fiddler模拟低速网络环境
有时候宽频网路用习惯了… 在开发的过程就比较少去考虑最佳化的问题… 但当有人反应说「你的网页好慢」甚至当网路速度慢,会造成你的网页跳出什么啊哩不哒的bug时要如何重现呢? 我们可以用Fiddler 这 ...
- spider RPC过滤器
spider支持在请求执行前或完成后进行特殊处理,比如安全性检查.敏感字段混淆等等.为此,spider提供了BeforeFilter和AfterFilter.其执行位置如下图所示: 流水线插件配置在s ...
- React Native:使用 JavaScript 构建原生应用
[转载] 本篇为联合翻译,译者:寸志,范洪春,kmokidd,姜天意 数月前,Facebook 对外宣布了正在开发的 React Native 框架,这个框架允许你使用 JavaScript 开发原生 ...
- IT软件技术人员的职位路线(从程序员到技术总监) - 部门管理经验谈
以前写过一个文(IT从业者的职业道路(从程序员到部门经理) - 项目管理系列文章),主要介绍笔者的职业发展之路,不过该文需要后续了,因为笔者现在从事的是“产品经理”一职.从笔者的导航文([置顶]博文快 ...
- SQL优化技术分析-1:操作符优化
1.IN 操作符 用IN写出来的SQL的优点是比较容易写及清晰易懂,这比较适合现代软件开发的风格.但是用IN的SQL 性能总是比较低的,从Oracle执行的步骤来分析用IN的SQL与不用IN的SQL有 ...
- [Erlang 0127] Term sharing in Erlang/OTP 上篇
之前,在 [Erlang 0126] 我们读过的Erlang论文 提到过下面这篇论文: On Preserving Term Sharing in the Erlang Virtual Machine ...
- Oracle XMLTable 使用教程与实例
从Oracle 10g开始,甲骨文公司新增了XQuery和XMLTable两个功能作为处理XML的武器. XMLQuery一样,您可以使用XQuery语言构造XML数据和查询XML和关系数据.你可以使 ...
- 由一个bug引发的SQLite缓存一致性探索
问题 我们在生产环境中使用SQLite时中发现建表报“table xxx already exists”错误,但DB文件中并没有该表.后面才发现这个是SQLite在实现过程中的一个bug,而这个bug ...