题目戳这里

这题如果用\(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. hdu_5288_OO’s Sequence

    OO has got a array A of size n ,defined a function f(l,r) represent the number of i (l<=i<=r) ...

  2. docker镜像文件导入与导出 , 支持批量

    1. 查看镜像id sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE quay.io/calico/node v1.0.1 c70511a ...

  3. tomcat日志切割脚本

    tomcat日志每俩小时切割的脚本如下(这是用定时任务来完成的,此方法无需重启tomcat): time=$(date +%H) end_time=`` a=$end_time BF_TIME=$(- ...

  4. Java OOP——第五章 异常

    1. 尝试通过if-else来解决异常问题: Eg: public class Test2 {       public static void main(String[] args) {       ...

  5. php动态画图

    index.php <?php $width=800; $height=600; //绘图技术 基本步骤 前提:在php.ini文件中启用gd库 //创建画布 默认背景是黑色的 $img=ima ...

  6. 3.4.2 Undefined类型【JavaScript高级程序设计第三版】

    Undefined 类型只有一个值,即特殊的 undefined.在使用 var 声明变量但未对其加以初始化时,这个变量的值就是 undefined,例如: var message; alert(me ...

  7. php结合redis实现高并发下的抢购、秒杀功能【转】

    抢购.秒杀是如今很常见的一个应用场景,主要需要解决的问题有两个:1 高并发对数据库产生的压力2 竞争状态下如何解决库存的正确减少("超卖"问题)对于第一个问题,已经很容易想到用缓存 ...

  8. UINavigationController相关

    掌握: 1. UINavigationController的使用:添加.移除控制器. 2. UINavigationBar内容的设置. -------------------------------- ...

  9. activity堆栈式管理

    package com.chinaCEB.cebActivity.utils; import java.util.Stack; import android.app.Activity; import ...

  10. 【Spiral Matrix】cpp

    题目: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spira ...