Uva10635 Prince and Princess
2017-01-24 00:19
295 查看
题目戳这里
这题如果用\(f_{i,j}\)这样dp的话肯定过不了,必须另辟蹊径。题目说了数字不重复。我们先只留下两个数组共有的数字。然后我们处理出这样一个数组\(S\),\(S_i\)表示\(A_i\)这个元素在\(B\)中的下标,然后模型转换就成为了求\(S\)中最长上升子序列了,这个\(O(NlogN)\)的求法大家应该都会。这里我写的是树状数组版本的。
这题如果用\(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; }
相关文章推荐
- 手把手教你硬件raid5组建图文指南
- swift不用声明类型那些事
- Linux网卡参数详解
- RadioButton和RadioGroup的使用
- LeetCode Game of Life 解题
- 模板队列-支持多线程
- bzoj2038: [2009国家集训队]小Z的袜子(hose)
- Hdu1423 Greatest Common Increasing Subsequence
- java栈的实现
- 有关Android的外部拉起
- C++ 线程-类方式
- C++网络编程
- 排序
- 2016书单总结--看透SpringMvc源代码分析与实践-概述
- c++思考题
- Windows DLL开发笔记
- C++ SQL 语句格式化
- 【PAT】1048. Find Coins
- 学习hibernate_02_双向onetoone注解
- 【机器学习】数据预处理