题目大意:给你$k(2\leqslant k\leqslant5)$个$1\sim n(n\leqslant10^3)$的排列,求它们的最长子序列

题解:将$k$个排列中每个元素的位置记录下来。如果是公共子序列,那么这些数的位置在$k$个排列中都是递增的,然后就变成了一个$k$维偏序问题。

因为$n\leqslant10^3$,所以可以用$O(n^2k)$的$DP$来做

卡点:看成了最长公共上升子序列,然后一直挂

C++ Code:

#include <cstdio>
#define maxn 1010
int n, k, ans;
int s[5][maxn], pos[5][maxn];
int f[maxn];
inline int max(int a, int b) {return a > b ? a : b;}
int main() {
scanf("%d%d", &n, &k);
for (int i = 0; i < k; i++) {
for (int j = 1; j <= n; j++) {
scanf("%d", s[i] + j);
pos[i][s[i][j]] = j;
}
}
for (int i = 1, v; v = s[0][i], i <= n; i++) {
f[v] = 1;
for (int j = 1, u; u = s[0][j], j < i; j++) {
int found = 20040826;
for (int l = 1; l < k && found; l++) if (pos[l][v] < pos[l][u]) found = 0;
if (found) f[v] = max(f[v], f[u] + 1);
}
ans = max(ans, f[v]);
}
printf("%d\n", ans);
return 0;
}

  

[CF463D]Gargari and Permutations的更多相关文章

  1. CF463D Gargari and Permutations dp

    给定 $n<=10$ 个 $1$~$n$ 的排列,求这些排列的 $LCS$. 考虑两个排列怎么做:以第一个序列为基准,将第二个序列的元素按照该元素在第一个序列中出现位置重新编号. 然后,求一个 ...

  2. Codeforces #264 (Div. 2) D. Gargari and Permutations

    Gargari got bored to play with the bishops and now, after solving the problem about them, he is tryi ...

  3. codeforces Gargari and Permutations(DAG+BFS)

    /* 题意:求出多个全排列的lcs! 思路:因为是全排列,所以每一行的每一个数字都不会重复,所以如果有每一个全排列的数字 i 都在数字 j的前面,那么i, j建立一条有向边! 最后用bfs遍历整个图, ...

  4. codeforces 463D Gargari and Permutations(dp)

    题目 参考网上的代码的... //要找到所有序列中的最长的公共子序列, //定义状态dp[i]为在第一个序列中前i个数字中的最长公共子序列的长度, //状态转移方程为dp[i]=max(dp[i],d ...

  5. Codeforces 463D Gargari and Permutations

    http://codeforces.com/problemset/problem/463/D 题意:给出k个排列,问这k个排列的最长公共子序列的长度. 思路:只考虑其中一个的dp:f[i]=max(f ...

  6. Codeforces Round #264 (Div. 2) D. Gargari and Permutations 多序列LIS+dp好题

    http://codeforces.com/contest/463/problem/D 求k个序列的最长公共子序列. k<=5 肯定 不能直接LCS 网上题解全是图论解法...我就来个dp的解法 ...

  7. Codeforces 463D Gargari and Permutations(求k个序列的LCS)

    题目链接:http://codeforces.com/problemset/problem/463/D 题目大意:给你k个序列(2=<k<=5),每个序列的长度为n(1<=n< ...

  8. CF 463D Gargari and Permutations [dp]

    给出一个长为n的数列的k个排列(1 ≤ n ≤ 1000; 2 ≤ k ≤ 5).求这个k个数列的最长公共子序列的长度 dp[i]=max{dp[j]+1,where j<i 且j,i相应的字符 ...

  9. Codeforces 463D Gargari and Permutations:隐式图dp【多串LCS】

    题目链接:http://codeforces.com/problemset/problem/463/D 题意: 给你k个1到n的排列,问你它们的LCS(最长公共子序列)是多长. 题解: 因为都是1到n ...

随机推荐

  1. Q&A - Apache、Nginx与Tomcat的区别?

    一.     定义: 1.     Apache Apache HTTP服务器是一个模块化的服务器,可以运行在几乎所有广泛使用的计算机平台上.其属于应用服务器.Apache支持支持模块多,性能稳定,A ...

  2. 文件权限管理命令chmod,chown与文本搜索命令grep

    1.复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限. [root@bogon home]# cp -r /etc/sk ...

  3. 设置vim tab为4个空格

    Vim 编辑器默认tab为8个空格,但对于pythoner来说,必须要调整到4个空格. 方法如下: 在~/.vimrc文件中加入下面设置: set ts=4 #设置tabstop为4个空格 set e ...

  4. Windows下如何安装composer

    相对 来说并不难直接将此文件下载安装即可 1 https://getcomposer.org/Composer-Setup.exe 文件地址由官方提供 https://getcomposer.org/ ...

  5. PLY手册翻译

    https://www.kancloud.cn/kancloud/ply/42143 http://wiki.jikexueyuan.com/project/python-lex-yacc/ply-0 ...

  6. linux通用GPIO驱动,写GPIO文件不立即生效问题解决

    Linux开发平台实现了通用GPIO的驱动,用户通过,SHell或者系统调用能控制GPIO的输出和读取其输入值.其属性文件均在/sys/class/gpio/目录下,该目录下有export和unexp ...

  7. 笔记-docker-2安装(centos6.5环境)

    笔记-docker-2安装(centos6.5环境) 1.      centos6.5安装docker 1.1.    升级内核 安装docker,官方文档要求linux kernel至少3.8以上 ...

  8. hadoop中namenode发生故障的处理方法

    Namenode 故障后,可以采用如下两种方法恢复数据: 方法一:将 SecondaryNameNode 中数据拷贝到 namenode 存储数据的目录: 方法 二: 使用 -importCheckp ...

  9. bootstrap重新设计按钮样式

    1.将btn的样式换成以下的样式 2.思路: (1)将原来的btn样式设置color:#FFF,同时text-shadow设置,这样原来的btn样式就会变淡了,后面再加上新的样式就可以覆盖掉 注意:要 ...

  10. laravel - ReflectionException in Container.php, Class not found?

    SIGN UPSIGN IN CATALOG SERIES PODCAST DISCUSSIONS ReflectionException in Container.php, Class not fo ...