传送门

无重复元素的LCS问题

n2 做法不说了。

nlogn 做法 ——

因为LCS问题求的是公共子序列,顺序不影响答案,影响答案的只是两个串的元素是否相同,所以可以交换元素位置。

首先简化一下问题,假设P1恰好为单调递增的1,2,3,...n,那么很显然答案就是P2的最长上升子序列的长度

问题是P1并非单调递增的,但我们可以假定它就是1,2,3,...,n。

也就是重新定义一下第一个串中 所有数 的顺序,定义a[x] = i,也就是 数x 是第 i 个,然后再重新弄一下第二串的顺序,最后求一遍lis。

——代码

 #include <cstdio>
#include <algorithm>
#include <cstring> using namespace std; const int MAXN = ;
int n, ans;
int a[MAXN], b[MAXN], c[MAXN]; inline int query(int x)
{
int ans = ;
for(; x; x -= x & -x) ans = max(ans, c[x]);
return ans;
} inline void update(int x, int d)
{
for(; x <= n; x += x & -x) c[x] = max(c[x], d);
} int main()
{
int i, j, x, y;
scanf("%d", &n);
for (i = ; i <= n; i++) scanf("%d", &x), a[x] = i;
for (i = ; i <= n; i++) scanf("%d", &x), b[i] = a[x];
for(i = ; i <= n; i++)
{
y = query(b[i] - ) + ;
update(b[i], y);
ans = max(ans, y);
}
printf("%d", ans);
return ;
}

还有另一种思路。也是 nlogn,而且比较好理解。(说实话,我真不理解上面的映射是怎么弄的)

原本 n做法是设 f[i][j] 表示 第一串的前 i 个数 和 第二串的前 j 个数 的最优答案(i 和 j 都不必须选),然后一阵乱搞。

nlogn——

可以改变状态的定义,f[i][j] 表示 第一串的前 i 个数 和 第二串的前 j 个数 的最有答案(i 不必须选,j 必须选

这样 f[i][] 只能由 f[i - 1][] 转移过来,这样就变成了分层的DP,并且只转移到 f[i][k] (其中 b[k] == a[i]),也就是只影响一个答案。

所以先记录和 a[i] 相同的 b[j] 的位置,然后 f 数组可以变成一维,动态维护 f 数组即可。

f[i] = max(f[j]) + 1 ( 1 <= j  < i && a[i] == b[j])

——代码

 #include <cstdio>
#include <iostream> using namespace std; const int MAXN = ;
int n, ans;
int a[MAXN], b[MAXN], c[MAXN], p[MAXN], f[MAXN]; inline int query(int x)
{
int ret = ;
for(; x; x -= x & -x) ret = max(ret, c[x]);
return ret;
} inline void update(int x, int d)
{
for(; x <= n; x += x & -x) c[x] = max(c[x], d);
} int main()
{
int i;
scanf("%d", &n);
for(i = ; i <= n; i++) scanf("%d", &a[i]);
for(i = ; i <= n; i++) scanf("%d", &b[i]), p[b[i]] = i;
for(i = ; i <= n; i++)
{
f[p[a[i]]] = query(p[a[i]] - ) + ;
update(p[a[i]], f[p[a[i]]]);
ans = max(ans, f[p[a[i]]]);
}
printf("%d", ans);
return ;
}

[luoguP1439] 排列LCS问题(DP + 树状数组)的更多相关文章

  1. bzoj 1264 [AHOI2006]基因匹配Match(DP+树状数组)

    1264: [AHOI2006]基因匹配Match Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 793  Solved: 503[Submit][S ...

  2. 树形DP+树状数组 HDU 5877 Weak Pair

    //树形DP+树状数组 HDU 5877 Weak Pair // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 // 这道题要离散化 #i ...

  3. 【bzoj2274】[Usaco2011 Feb]Generic Cow Protests dp+树状数组

    题目描述 Farmer John's N (1 <= N <= 100,000) cows are lined up in a row andnumbered 1..N. The cows ...

  4. 奶牛抗议 DP 树状数组

    奶牛抗议 DP 树状数组 USACO的题太猛了 容易想到\(DP\),设\(f[i]\)表示为在第\(i\)位时方案数,转移方程: \[ f[i]=\sum f[j]\;(j< i,sum[i] ...

  5. 第十四个目标(dp + 树状数组 + 线段树)

    Problem 2236 第十四个目标 Accept: 17    Submit: 35 Time Limit: 1000 mSec    Memory Limit : 32768 KB  Probl ...

  6. Codeforces 1096F(dp + 树状数组)

    题目链接 题意: 对于长度为$n$的排列,在已知一些位的前提下求逆序对的期望 思路: 将答案分为$3$部分 $1.$$-1$与$-1$之间对答案的贡献.由于逆序对考虑的是数字之间的大小关系,故假设$- ...

  7. [CF1086E]Beautiful Matrix(容斥+DP+树状数组)

    给一个n*n的矩阵,保证:(1)每行都是一个排列 (2)每行每个位置和上一行对应位置不同.求这个矩阵在所有合法矩阵中字典序排第几.考虑类似数位DP的做法,枚举第几行开始不卡限制,那么显然之前的行都和题 ...

  8. Codeforces 1085G(1086E) Beautiful Matrix $dp$+树状数组

    题意 定义一个\(n*n\)的矩阵是\(beautiful\)的,需要满足以下三个条件: 1.每一行是一个排列. 2.上下相邻的两个元素的值不同. 再定义两个矩阵的字典序大的矩阵大(从左往右从上到下一 ...

  9. BZOJ 4361 isn 容斥+dp+树状数组

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=4361 题意概述: 给出一个长度为N的序列A(A1,A2...AN).如果序列A不是非降的 ...

  10. codeforces 597C C. Subsequences(dp+树状数组)

    题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...

随机推荐

  1. Android 线程池系列教程(1)目录

    Sending Operations to Multiple Threads 1.Dependencies and prerequisites Android 3.0 (API Level 11) o ...

  2. webapp开发学习---Cordova环境搭建

    Cordova 使用HTML, CSS & JS进行移动App开发;多平台共用一套代码;免费开源 步骤:(来自Cordova官网) 1.安装Cordova(在node.js环境下进行安装) 命 ...

  3. jquery各种选择器示例

    $("#itemExpressionHidden>b:last")   选择id为itemExpressionHidden中的最后一个b标签 $("#itemExp ...

  4. poj3368 Frequent values

    思路: 转化为RMQ. 实现: #include <cstdio> #include <cstring> #include <algorithm> using na ...

  5. Spark学习之Spark SQL(8)

    Spark学习之Spark SQL(8) 1. Spark用来操作结构化和半结构化数据的接口--Spark SQL. 2. Spark SQL的三大功能 2.1 Spark SQL可以从各种结构化数据 ...

  6. BaseAdapter的优化

    传统的 package cct.commonadapter.bean; import android.content.Context; import android.view.LayoutInflat ...

  7. leetcode:single-number-ii(Java位运算)

    题目 Given an array of integers, every element appears three times except for one. Find that single on ...

  8. iOS programming UITableView and UITableViewController

    iOS programming  UITableView and UITableViewController A UITableView displays a single column of dat ...

  9. 使用Qt5.7.0 VS2015版本生成兼容XP的可执行程序

    版权声明:本文为灿哥哥http://blog.csdn.net/caoshangpa原创文章,转载请标明出处. 一.直接使用VS2012/VS2013/VS2015生成XP兼容的可执行程序 Visua ...

  10. 乐视max2 刷入第三方recovery 然后刷入root 包 root

    乐视max2 刷入第三方recovery 然后刷入root 包 root 第三方recovery:为奇兔 刷入root 包 https://share.weiyun.com/ddcdd5ea83956 ...