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 ...
随机推荐
- dts--tests(四)
unit_tests.py """ DPDK Test suite. This TestSuite runs the unit tests included in DPD ...
- 【Python3】操作文件,目录和路径
1.遍历文件夹和文件 Python代码 import os import os.path rootdir = "d:/test" for parent,dirnames,fi ...
- php-5.6.26源代码 - opcode处理器,“乘法opcode”处理器
// opcode处理器 - 运算符怎么执行: “*” 乘法opcode处理器 static int ZEND_FASTCALL ZEND_MUL_SPEC_CONST_CONST_HANDLER(Z ...
- E - Nature Reserve CodeForces - 1059D
传送门 There is a forest that we model as a plane and live nn rare animals. Animal number iihas its lai ...
- POJ:1703-Find them, Catch them(并查集好题)(种类并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 49867 Accepted: 153 ...
- WRITE命令 書式設定オプション
書式設定オプション WRITE 命令では.さまざまな書式設定オプションが使用することができます. 構文 WRITE ....f option. 全データ型の書式設定オプション オプション 機能 LEF ...
- sparkStreaming统计各平台最近一分钟实时注册收入 时间段,平台,金额,订单数
样例数据: __clientip=10.10.9.153&paymentstatus=0&__opip=&memberid=89385239&iamount=1& ...
- Storm: 集群安装和配置
前期准备:3台服务器: 192.168.8.94 192.168.8.95 192.168.8.96 去storm官网下载响应版本的软件包:http://storm.apache.org/downl ...
- eclipse包名分层级显示
eclipse包名像文件列表那样显示设置方法
- python基础——数字&集合&布尔类型
Python的核心数据类型 内置对象 对象类型 例子 数字 123,3.1415,3+4j,Decimal(小数),Fraction(分数) 字符串 'dodo',"guido's" ...