Uva10635 Prince and Princess
题目戳这里
这题如果用\(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的更多相关文章
- UVa10635 - Prince and Princess(LCS转LIS)
题目大意 有两个长度分别为p+1和q+1的序列,每个序列中的各个元素互不相同,且都是1~n^2之间的整数.两个序列的第一个元素均为1.求出A和B的最长公共子序列长度. 题解 这个是大白书上的例题,不过 ...
- UVA - 10635 Prince and Princess LCS转LIS
题目链接: http://bak.vjudge.net/problem/UVA-10635 Prince and Princess Time Limit: 3000MS 题意 给你两个数组,求他们的最 ...
- 强连通+二分匹配(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& ...
- uva 10635 - Prince and Princess(LCS)
题目连接:10635 - Prince and Princess 题目大意:给出n, m, k,求两个长度分别为m + 1 和 k + 1且由1~n * n组成的序列的最长公共子序列长的. 解题思路: ...
- Prince and Princess HDU - 4685(匹配 + 强连通)
Prince and Princess Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Othe ...
- HDU 4685 Prince and Princess 二分图匹配+tarjan
Prince and Princess 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4685 Description There are n pri ...
- 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 ...
随机推荐
- poj_1730_Perfect Pth Powers
We say that x is a perfect square if, for some integer b, x = b 2. Similarly, x is a perfect cube if ...
- 安装阿里云版Linux云服务器,配置软件
1. 购买域名 2. 购买云服务器ecs 3. 远程访问云服务器并装上Java环境和必备软件 3.1安装远程访问工具 3.2 jdk环境配置 3.3 Mysql依赖关系 重新配置MySQL的远程 ...
- 交换机基础配置之跨交换机划分vlan
我们以上面的拓扑图来进行实验 四台pc机都在同一网段 pc1和pc2在同一台交换机上 pc3和pc4在同一台交换机上 现在我们实验的目的就是将pc1和pc3划分到同一vlan pc2和pc4划分到同一 ...
- JAVAOOP异常
排序: Try-catch-finally:try正常执行,如果有异常执行catch后执行finally,如果没有直接执行finally 执行顺序:try-catch:try中的语句正常执行,如果遇到 ...
- PHP 使用GD库合成带二维码和圆形头像的海报步骤以及源码实现
PHP 使用GD库合成带二维码和圆形头像的海报步骤以及源码实现 之前记录过一篇文章,不过那只是简单将二维码合成到海报中去,这次还要合成头像,而且是圆形.所需要素材就不一一列举,直接代码吧 1.先获取用 ...
- SSO 单点登录总结(PHP)
本篇文章根据个人理解的知识整理汇总,如有不足之处,请大家多多指正. 单点登录(SSO--Single Sign On)的应用是很普遍的,尤其在大型网站系统中,比如百度,登录百度账号和,再转到百度经验. ...
- android onCreate的两个方法
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) { } override ...
- Internet接入方式
(转载) 接入网可以大概分成两类:拨号上网(包括ASDL)与专线上网 在接入网中,目前可供选择的接入方式主要有PSTN.ISDN.DDN.LAN.ADSL.VDSL.Cable-Modem.PON和L ...
- 查询数据库里当前用户下的所有表的总共数据sql
select t.table_name,t.num_rows from user_tables t select sum(num_rows) from user_tables t
- vue-i18n vue-cli项目中实现国际化 多语言切换功能 一
vue-cli项目中引入vue-i18n 安装: npm install vue-i18n可参考vue-i18n官网文档 main.js中引入: import Vue from 'vue' impor ...