http://codeforces.com/contest/463/problem/D

求k个序列的最长公共子序列。

k<=5

肯定 不能直接LCS

网上题解全是图论解法...我就来个dp的解法吧

首先,让我们创建的位置阵列的每个序列pos[k][N]。在这个数组,我们存储这n个数字(从1到n)在这k个序列中各个位置,然后按数大小排序。

DP [I]标记的最大长度共同序列,我们可以通过使用第一个序列的第i个元素生成。为此,我们遍历所有可能以前的编号(J),看看我们是否能延长DP [J]到DP [I]。

对于给定的J<I,只有在所有的序列中J的位置前与I,才能更新DP[I] = max(DP[J] + 1).

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <string>
  6. #include <queue>
  7. #include <vector>
  8. #include<set>
  9. #include <iostream>
  10. #include <algorithm>
  11. using namespace std;
  12. #define RD(x) scanf("%d",&x)
  13. #define RD2(x,y) scanf("%d%d",&x,&y)
  14. #define clr0(x) memset(x,0,sizeof(x))
  15. typedef long long LL;
  16. vector <int> pos[1005];
  17. int dp[1005];
  18. int main()
  19. {
  20. int n,k,x;
  21. RD2(n,k);
  22. for(int i = 0;i < k;++i){
  23. for(int j = 1;j <= n;++j){
  24. RD(x);
  25. pos[x].push_back(j);
  26. }
  27. }
  28. sort(pos+1,pos+n+1);
  29. int ans = 1;
  30. for(int i = 1;i <= n;++i)
  31. dp[i] = 1;
  32. for(int i = 2;i <= n;++i){
  33. for(int j = 1;j < i;++j){
  34. bool ok = true;
  35. for(int l = 0;l < k;++l){
  36. if(pos[j][l] > pos[i][l]){
  37. ok = false;
  38. break;
  39. }
  40. }
  41. if(ok){
  42. dp[i] = max(dp[j]+1,dp[i]);
  43. }
  44. }
  45. ans = max(ans,dp[i]);
  46. }
  47. printf("%d\n",ans);
  48. return 0;
  49. }

Codeforces Round #264 (Div. 2) D. Gargari and Permutations 多序列LIS+dp好题的更多相关文章

  1. Codeforces Round #264 (Div. 2) C. Gargari and Bishops 主教攻击

    http://codeforces.com/contest/463/problem/C 在一个n∗n的国际象棋的棋盘上放两个主教,要求不能有位置同时被两个主教攻击到,然后被一个主教攻击到的位置上获得得 ...

  2. Codeforces Round #264 (Div. 2) C Gargari and Bishops 【暴力】

    称号: 意甲冠军:给定一个矩阵,每格我们有一个数,然后把两个大象,我希望能够吃的对角线上的所有数字.我问两个最大的大象可以吃值. 分析:这种想法是暴力的主题,计算出每一格放象的话能得到多少钱,然后求出 ...

  3. Codeforces Round #485 (Div. 2) E. Petr and Permutations

    Codeforces Round #485 (Div. 2) E. Petr and Permutations 题目连接: http://codeforces.com/contest/987/prob ...

  4. Codeforces Round #539 (Div. 2) - C. Sasha and a Bit of Relax(思维题)

    Problem   Codeforces Round #539 (Div. 2) - C. Sasha and a Bit of Relax Time Limit: 2000 mSec Problem ...

  5. Codeforces Round #433 (Div. 2)【A、B、C、D题】

    题目链接:Codeforces Round #433 (Div. 2) codeforces 854 A. Fraction[水] 题意:已知分子与分母的和,求分子小于分母的 最大的最简分数. #in ...

  6. Codeforces Round #264 (Div. 2)

    http://codeforces.com/contest/463 这场是我人生第一场cf啊.. 悲剧处处是啊. 首先,看不懂题,完全理解不了啊.都是wa了好几次才过的 所以a和b这两sb题我做了1个 ...

  7. Codeforces Round #264 (Div. 2) C

    题目: C. Gargari and Bishops time limit per test 3 seconds memory limit per test 256 megabytes input s ...

  8. Codeforces Round #264 (Div. 2) E. Caisa and Tree 树上操作暴力

    http://codeforces.com/contest/463/problem/E 给出一个总节点数量为n的树,每个节点有权值,进行q次操作,每次操作有两种选项: 1. 询问节点v到root之间的 ...

  9. 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 ...

随机推荐

  1. IDEA 的 Edit 设置

    1.设置鼠标悬浮提示 General -- Show quick documentation on mouse move 2.自动导包 3.设置显示行号和方法的间隔符 4.忽略大小写  4.设置取消单 ...

  2. Luogu2161 [SHOI2009]会场预约-线段树

    Solution 线段树维护 sum 表示区间内预约个数, L 表示区间最左边的预约, R 表示区间最右边的预约. $pushup$ 就是这样 : void up(int nd) { sum[nd] ...

  3. sqli-labs:18-22,http头部注入

    sqli18: uname和passwd被处理了: uagent和ip插入到了数据库: 还带回显. 抓包改包 sqli19: null sqli20: 审计代码,大概如下 当我们正常登录后userna ...

  4. paxos 练手 推进中

    学习https://github.com/huoyu820125/SecondPaxos 自己编写网络版本 在学习过程将此代码的线程 锁等改成c++11  就不用包含那么多文件 主要更改如下 // M ...

  5. mysql cmd 无法登录

    第一次折腾mysql诉苦记 版本注明: mysql 5.7.21 本地部署mysql,配置完成后(配置没有问题) cmd命令连接mysql: mysql -uroot -p 提示: ERROR 104 ...

  6. activiti如何获取当前节点以及下一步路径或节点(转)

    ACTIVITI相对于JBPM来说,比较年轻,用的人少,中文方面的资料更少,我根据网上到处找得资料以及看官方文档总结出来了代码,非常不容易啊.废话不多说,直接上代码吧: 首先是根据流程ID获取当前任务 ...

  7. 检测鼠标是否在UI上unity

    public static bool IsCursorOnUI(int inputID=-1){ EventSystem eventSystem = EventSystem.current; retu ...

  8. Ajax复习

    1.标准请求响应时浏览器的动作(同步操作) 1.1 浏览器请求什么资源,跟随显示什么资源 2.ajax:异步请求: 2.1 局部刷新,通过异步请求,请求到服务器资源数据后,通过脚本修改页面中部分内容 ...

  9. 33、iOS10 由于权限问题导致崩溃的大坑

    控制台报忠告: This app has crashed because it attempted to access privacy-sensitive data without a usage d ...

  10. ssh 常用命令

    1.复制SSH密钥到目标主机,开启无密码SSH登录 ssh-copy-id user@host 如果还没有密钥,请使用ssh-keygen命令生成. 2.从某主机的80端口开启到本地主机2001端口的 ...