UVa 10635 (LIS+二分) Prince and Princess
题目的本意是求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的更多相关文章
- UVA - 10635 LIS LCS转换
白书例题,元素互不相同通过哈希转换为LIS求LCS #include<iostream> #include<algorithm> #include<cstdio> ...
- UVA - 10635 Prince and Princess LCS转LIS
题目链接: http://bak.vjudge.net/problem/UVA-10635 Prince and Princess Time Limit: 3000MS 题意 给你两个数组,求他们的最 ...
- UVA 10635 Prince and Princess—— 求LCS(最长公共子序列)转换成 求LIS(最长递增子序列)
题目大意:有n*n个方格,王子有一条走法,依次经过m个格子,公主有一种走法,依次经过n个格子(不会重复走),问他们删去一些步数后,重叠步数的最大值. 显然是一个LCS,我一看到就高高兴兴的打了个板子上 ...
- uva 10635 - Prince and Princess(LCS)
题目连接:10635 - Prince and Princess 题目大意:给出n, m, k,求两个长度分别为m + 1 和 k + 1且由1~n * n组成的序列的最长公共子序列长的. 解题思路: ...
- 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 ...
- UVA 10653.Prince and Princess
题目 In an n * n chessboard, Prince and Princess plays a game. The squares in the chessboard are numbe ...
- 强连通+二分匹配(hdu4685 Prince and Princess)
Prince and Princess Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Othe ...
- 10635 - Prince and Princess
Problem D Prince and Princess Input: Standard Input Output: Standard Output Time Limit: 3 Seconds In ...
- UVa10653.Prince and Princess
题目连接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
随机推荐
- ATT GATT Profile
Bluetooth: ATT and GATT Bluetooth 4.0, which includes the Low Energy specification, brings two new c ...
- 由浅入深了解Thrift之客户端连接池化
一.问题描述 在上一篇<由浅入深了解Thrift之服务模型和序列化机制>文章中,我们已经了解了thrift的基本架构和网络服务模型的优缺点.如今的互联网圈中,RPC服务化的思想如火如荼.我 ...
- POJ 1455
/* 冒泡排序n*(n-1)/2; */ #include <iostream> using namespace std; int main() { //freopen("acm ...
- POJ 1504
#include<iostream> using namespace std; int main() { int num; cin>>num; while(num--){ in ...
- POJ 1947 Rebuilding Roads(树形DP)
题目链接 题意 : 给你一棵树,问你至少断掉几条边能够得到有p个点的子树. 思路 : dp[i][j]代表的是以i为根的子树有j个节点.dp[u][i] = dp[u][j]+dp[son][i-j] ...
- JS面向(基于)对象编程--构造方法(函数)
构造函数(方法)介绍 什么是构造函数呢?在回答这个问题之前,我们来看一个需求:前面我们在创建人类的对象时,是先把一个对象创建好后,再给他的年龄和姓名属性赋值,如果现在我要求,在创建人类的对象时,就直接 ...
- 安卓四大组件之--service
服务:长期后台运行的没有界面的activity,程序写法和activity类似. 安卓系统进程管理是按照一定规则的: 1.默认情况下,关闭掉一个应用程序,清空了这个应用程序的任务栈,应用程序的进程还会 ...
- switch中的default的位置
[转载]http://hi.baidu.com/dannie007zxl/item/5d0c3185577df719c3162724 有的时候,我们对身旁自认为熟悉的东西,却发现很难去给出准确的回答. ...
- haproxy重启
ps -aux | grep haproxy.cfg sudo kill -9 pidsudo /data/tools/haproxy/sbin/haproxy -f /data/tools/hapr ...
- Java-J2SE学习笔记-线程-生产者消费者问题
一.概述 模拟生产者消费者问题 二.代码 1.Consumer.java 2.Producer.java 3.SyncStack.java 4.Test.java 1.Consumer.java pa ...