题目戳这里

这题如果用\(f_{i,j}\)这样dp的话肯定过不了,必须另辟蹊径。题目说了数字不重复。我们先只留下两个数组共有的数字。然后我们处理出这样一个数组\(S\),\(S_i\)表示\(A_i\)这个元素在\(B\)中的下标,然后模型转换就成为了求\(S\)中最长上升子序列了,这个\(O(NlogN)\)的求法大家应该都会。这里我写的是树状数组版本的。

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std; #define lowbit(x) (x&-x)
const int maxn = 250*250+10;
int pos[maxn],S[maxn],T,tree[maxn],N,P,Q,cnt,ans; inline void ins(int a,int b) { for (;a <= N*N;a += lowbit(a)) tree[a] = max(tree[a],b); }
inline int calc(int a) { int ret = 0; for (;a;a -= lowbit(a)) ret = max(ret,tree[a]); return ret; } inline int read()
{
int ret = 0,f = 1; char ch;
do ch = getchar(); while (!(ch >= '0'&&ch <= '9')&&ch != '-');
if (ch == '-') ch = getchar(),f = -1;
do ret = ret*10+ch-'0',ch = getchar(); while (ch >= '0'&&ch <= '9');
return ret*f;
} int main()
{
freopen("10635.in","r",stdin);
freopen("10635.out","w",stdout);
scanf("%d",&T);
for (int Case = 1;Case <= T;++Case)
{
printf("Case %d: ",Case);
N = read(); P = read()+1; Q = read()+1; cnt = ans = 0;
for (int i = 1;i <= N*N;++i) pos[i] = tree[i] = 0;
for (int i = 1;i <= P;++i) pos[read()] = i;
for (int i = 1,b;i <= Q;++i)
{
b = read();
if (pos[b]) S[++cnt] = pos[b];
}
for (int i = 1;i <= cnt;++i)
{
int f = calc(S[i]-1)+1;
ans = max(ans,f); ins(S[i],f);
}
printf("%d\n",ans);
}
fclose(stdin); fclose(stdout);
return 0;
}

Uva10635 Prince and Princess的更多相关文章

  1. UVa10635 - Prince and Princess(LCS转LIS)

    题目大意 有两个长度分别为p+1和q+1的序列,每个序列中的各个元素互不相同,且都是1~n^2之间的整数.两个序列的第一个元素均为1.求出A和B的最长公共子序列长度. 题解 这个是大白书上的例题,不过 ...

  2. UVA - 10635 Prince and Princess LCS转LIS

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

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

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

  4. 10635 - Prince and Princess

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

  5. UVa10653.Prince and Princess

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

  6. uva 10635 - Prince and Princess(LCS)

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

  7. Prince and Princess HDU - 4685(匹配 + 强连通)

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

  8. HDU 4685 Prince and Princess 二分图匹配+tarjan

    Prince and Princess 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4685 Description There are n pri ...

  9. 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 ...

随机推荐

  1. SHGetSpecialFolderLocation获取开始文件夹

    SHGetSpecialFolderLocation函数可以获取windows 特殊目录 函数原型:(MSDN官方链接:https://msdn.microsoft.com/en-us/library ...

  2. MySQL - Mac下安装MySQL

    1. 去官网下载dmg的安装文件. 2. 下载完成后,运行安装文件,按步骤进行安装,安装完成后会弹出一个框显示临时密码! 3. 编辑~/.bashrc文件,配置快速启动/停止/重启/cdhome/别名 ...

  3. 使用CSS隐藏HTML元素的四种常用方法

    CSS隐藏HTML元素的四种常用方法 1.opacity:设置opacity: 0可以使一个元素变得完全透明. 设置的透明度会被子元素继承,而且无法取消. 通常可以使用opacity属性来制作元素的淡 ...

  4. .c和.h区别

    本质没有区别: .h是头文件 一般情况下下边内容放在.h文件中 宏定义 结构体,联合,枚举声明 typedef声明 外部函数声明 全局变量声明 .c是程序文件 一般情况下下边内容放在.h文件中 内含函 ...

  5. python pycharm2018开启debug模式

    为什么需要开启DEBUG模式1.如果开启了DEBUG模式,那么在代码中如果抛出了异常,在浏览器的页面中可以看到具体的错误信息,以及具体的错误代码位置,方便开发者调试.2.如果开启DEBUG模式,那么以 ...

  6. Pandas 数据读取

    1.读取table # 读取普通分隔数据:read_table # 可以读取txt,csv import os os.chdir('F:/') #首先设置一下读取的路径 data1 = pd.read ...

  7. [CodeForces - 296D]Greg and Graph(floyd)

    Description 题意:给定一个有向图,一共有N个点,给邻接矩阵.依次去掉N个节点,每一次去掉一个节点的同时,将其直接与当前节点相连的边和当前节点连出的边都需要去除,输出N个数,表示去掉当前节点 ...

  8. NumPy库入门

    ndarray数组的元素类型 ndarray数组的创建 ndarray数组的操作 ndarray数组的运算

  9. Android启动屏全屏显示

    1.为首页面设置一个theme <style name="app_start" > <item name="android:windowNoTitle& ...

  10. CentOS 6.0 VNC远程桌面配置[转]

    原文出处: http://blog.haohtml.com/archives/12281 谢谢作者. 引言:必须明白:vncserver在调用的时候,会根据你的配置来启用server端的监听端口,端口 ...