题目的本意是求LCS,但由于每个序列的元素各不相同,所以将A序列重新编号{1,2,,,p+1},将B序列重新编号,分别为B中的元素在A中对应出现的位置(没有的话就是0)。

在样例中就是A = {1 7 5 4 8 3 9},B = {1 4 3 5 6 2 8 9}

重新编号以后:

A = {1 2 3 4 5 6 7}, B = {1 4 6 3 0 0 5 7}(里面的0在求LIS时可以忽略)

这样求A、B的LCS就转变为求B的LIS

求LIS用二分优化,时间复杂度为O(nlogn)

第一次做的用二分求LIS的题是HDU 1025

http://www.cnblogs.com/AOQNRMGYXLMV/p/3862139.html

在这里再复习一遍

 //#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = * ;
int num[maxn], s[maxn], dp[maxn]; int main(void)
{
#ifdef LOCAL
freopen("10635in.txt", "r", stdin);
#endif int T, kase;
scanf("%d", &T);
for(kase = ; kase <= T; ++kase)
{
int N, p, q, x;
scanf("%d%d%d", &N, &p, &q);
memset(num, , sizeof(num));
for(int i = ; i <= p+; ++i)
{
scanf("%d", &x);
num[x] = i;
}
int n = ;
for(int i = ; i <= q+; ++i)
{
scanf("%d", &x);
if(num[x])
s[n++] = num[x];
}
//求s[1]...s[n]的LIS
dp[] = s[];
int len = ;
for(int i = ; i <= n; ++i)
{
int left = , right = len;
while(left <= right)
{
int mid = (left + right) / ;
if(dp[mid] < s[i])
left = mid + ;
else
right = mid - ;
}
dp[left] = s[i];
if(left > len)
++len;
} printf("Case %d: %d\n", kase, len);
}
return ;
}

代码君

大白书里面用到了lower_bound函数

函数介绍

lower_bound()返回一个 iterator 它指向在[first,last)标记的有序序列中可以插入value,而不会破坏容器顺序的第一个位置,而这个位置标记了一个大于value 的值。

效果是一样的

 //#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int INF = ;
const int maxn = * ;
int num[maxn], s[maxn], g[maxn], d[maxn]; int main(void)
{
#ifdef LOCAL
freopen("10635in.txt", "r", stdin);
#endif int T, kase;
scanf("%d", &T);
for(kase = ; kase <= T; ++kase)
{
int N, p, q, x;
scanf("%d%d%d", &N, &p, &q);
memset(num, , sizeof(num));
for(int i = ; i <= p+; ++i)
{
scanf("%d", &x);
num[x] = i;
}
int n = ;
for(int i = ; i <= q+; ++i)
{
scanf("%d", &x);
if(num[x])
s[n++] = num[x];
}
//求s[1]...s[n]的LIS
for(int i = ; i <= n; ++i)
g[i] = INF;
int ans = ;
for(int i = ; i < n; ++i)
{
int k = lower_bound(g+, g+n+, s[i]) - g;
d[i] = k;
g[k] = s[i];
ans = max(ans, d[i]);
}
printf("Case %d: %d\n", kase, ans);
}
return ;
}

代码君

UVa 10635 (LIS+二分) Prince and Princess的更多相关文章

  1. UVA - 10635 LIS LCS转换

    白书例题,元素互不相同通过哈希转换为LIS求LCS #include<iostream> #include<algorithm> #include<cstdio> ...

  2. UVA - 10635 Prince and Princess LCS转LIS

    题目链接: http://bak.vjudge.net/problem/UVA-10635 Prince and Princess Time Limit: 3000MS 题意 给你两个数组,求他们的最 ...

  3. UVA 10635 Prince and Princess—— 求LCS(最长公共子序列)转换成 求LIS(最长递增子序列)

    题目大意:有n*n个方格,王子有一条走法,依次经过m个格子,公主有一种走法,依次经过n个格子(不会重复走),问他们删去一些步数后,重叠步数的最大值. 显然是一个LCS,我一看到就高高兴兴的打了个板子上 ...

  4. uva 10635 - Prince and Princess(LCS)

    题目连接:10635 - Prince and Princess 题目大意:给出n, m, k,求两个长度分别为m + 1 和 k + 1且由1~n * n组成的序列的最长公共子序列长的. 解题思路: ...

  5. HDU 4685 Prince and Princess (2013多校8 1010题 二分匹配+强连通)

    Prince and Princess Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Othe ...

  6. UVA 10653.Prince and Princess

    题目 In an n * n chessboard, Prince and Princess plays a game. The squares in the chessboard are numbe ...

  7. 强连通+二分匹配(hdu4685 Prince and Princess)

    Prince and Princess Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Othe ...

  8. 10635 - Prince and Princess

    Problem D Prince and Princess Input: Standard Input Output: Standard Output Time Limit: 3 Seconds In ...

  9. UVa10653.Prince and Princess

    题目连接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

随机推荐

  1. javascript实现数据结构与算法系列:循环链表与双向链表

    循环链表(circular linked list) 是另一种形式的链式存储结构.它的特点是表中最后一个结点的指针域指向头结点,整个表形成一个环. 循环链表的操作和线性链表基本一致,仅有细微差别. w ...

  2. Sqli-labs less 61

    Less-61 此处对于id处理还是有点奇葩的,第一次遇到利用两层括号的.(可能我头发比较长,见识短了).形式和上述是一样的 payload: http://127.0.0.1/sqli-labs/L ...

  3. PHP 性能分析与实验——性能的宏观分析

    [编者按]此前,阅读过了很多关于 PHP 性能分析的文章,不过写的都是一条一条的规则,而且,这些规则并没有上下文,也没有明确的实验来体现出这些规则的优势,同时讨论的也侧重于一些语法要点.本文就改变 P ...

  4. Javascript获取URL参数值

    getQueryString: function (name) { var reg = new RegExp("(^|&)" + name.toLowerCase() + ...

  5. HDU 2709 Sumsets(递推)

    Sumsets http://acm.hdu.edu.cn/showproblem.php?pid=2709 Problem Description Farmer John commanded his ...

  6. java基础知识回顾之接口

    /* abstract class AbsDemo { abstract void show1(); abstract void show2(); } 当一个抽象类中的方法都是抽象的时候,这时可以将该 ...

  7. 安装mysql之后,存入中文出现乱码 02

    现在出现这个问题,将编码字符串改成utf8 之后 数据表 还是不能存储中文. 看如下两张图,应该会有启发: 这下应该明白了吧.

  8. C# 6 —— 属性

    记录一下 C# 6 有关属性的语法糖实现,C# 6 涉及到属性的新特性主要有 2 个:自动属性初始化.只读属性赋值与读取. 自动属性初始化(Auto-property initializers) C# ...

  9. OnClientClick="return confirm('确定要删除吗?')"

    OnClientClick="return confirm('确定要删除吗?')"   -----------------------前台代码 OnClientClick用于执行客 ...

  10. MongoDB (一) MongoDB 介绍

    MongoDB 是一个跨平台的,面向文档的数据库,提供高性能,高可用性和可扩展性方便. MongoDB工作在收集和文件的概念. 数据库 数据库是一个物理容器集合.每个数据库都有自己的一套文件系统上的文 ...