Codeforces Round #264 (Div. 2) D. Gargari and Permutations 多序列LIS+dp好题
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).
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include<set>
#include <iostream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define clr0(x) memset(x,0,sizeof(x))
typedef long long LL;
vector <int> pos[1005];
int dp[1005];
int main()
{
int n,k,x;
RD2(n,k);
for(int i = 0;i < k;++i){
for(int j = 1;j <= n;++j){
RD(x);
pos[x].push_back(j);
}
}
sort(pos+1,pos+n+1);
int ans = 1;
for(int i = 1;i <= n;++i)
dp[i] = 1;
for(int i = 2;i <= n;++i){
for(int j = 1;j < i;++j){
bool ok = true;
for(int l = 0;l < k;++l){
if(pos[j][l] > pos[i][l]){
ok = false;
break;
}
}
if(ok){
dp[i] = max(dp[j]+1,dp[i]);
}
}
ans = max(ans,dp[i]);
}
printf("%d\n",ans);
return 0;
}
Codeforces Round #264 (Div. 2) D. Gargari and Permutations 多序列LIS+dp好题的更多相关文章
- Codeforces Round #264 (Div. 2) C. Gargari and Bishops 主教攻击
http://codeforces.com/contest/463/problem/C 在一个n∗n的国际象棋的棋盘上放两个主教,要求不能有位置同时被两个主教攻击到,然后被一个主教攻击到的位置上获得得 ...
- Codeforces Round #264 (Div. 2) C Gargari and Bishops 【暴力】
称号: 意甲冠军:给定一个矩阵,每格我们有一个数,然后把两个大象,我希望能够吃的对角线上的所有数字.我问两个最大的大象可以吃值. 分析:这种想法是暴力的主题,计算出每一格放象的话能得到多少钱,然后求出 ...
- Codeforces Round #485 (Div. 2) E. Petr and Permutations
Codeforces Round #485 (Div. 2) E. Petr and Permutations 题目连接: http://codeforces.com/contest/987/prob ...
- 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 ...
- Codeforces Round #433 (Div. 2)【A、B、C、D题】
题目链接:Codeforces Round #433 (Div. 2) codeforces 854 A. Fraction[水] 题意:已知分子与分母的和,求分子小于分母的 最大的最简分数. #in ...
- Codeforces Round #264 (Div. 2)
http://codeforces.com/contest/463 这场是我人生第一场cf啊.. 悲剧处处是啊. 首先,看不懂题,完全理解不了啊.都是wa了好几次才过的 所以a和b这两sb题我做了1个 ...
- Codeforces Round #264 (Div. 2) C
题目: C. Gargari and Bishops time limit per test 3 seconds memory limit per test 256 megabytes input s ...
- Codeforces Round #264 (Div. 2) E. Caisa and Tree 树上操作暴力
http://codeforces.com/contest/463/problem/E 给出一个总节点数量为n的树,每个节点有权值,进行q次操作,每次操作有两种选项: 1. 询问节点v到root之间的 ...
- 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 ...
随机推荐
- C# System.Data.OracleClient requires Oracle client software version 8.1.7 or greater
好好的程序,突然出现了错误,原因是:System.Data.OracleClient requires Oracle client software version 8.1.7 or greater, ...
- Linux CentOS 7 下 Apache Tomcat 7 安装与配置
前言 记录一下Linux CentOS 7安装Tomcat7的完整步骤. 下载 首先需要下载tomcat7的安装文件,地址如下: http://mirror.bit.edu.cn/apache/tom ...
- PHP 异步使用swoole的可行性测试
PHP的异步.并行.高性能网络通信引擎swoole框架,在一开始我就比较注意,原因无他,php在swoole未出的情况下在多线程调度上确实算得上没有较好的解决方案. 我以系统的注册流程举例,在比较复杂 ...
- spring读取properties的几种方式
参考链接:http://www.cnblogs.com/zxf330301/p/6184139.html
- HDOJ2586 How far away ?
一道LCA模板 原题链接 \(LCA\)模板题,不解释. 倍增版 #include<cstdio> #include<cmath> #include<cstring> ...
- zabbix监控系统_监控收集脚本使用分享
性能测试总是要监控服务器,做了zabbix监控之后,重要收集监控数据,这里分享下我是怎么做的. 准备文件 python2.7 pypa-setuptools.tar.gz -p -path /h ...
- Spring 属性注入(二)BeanWrapper 结构
Spring 属性注入(二)BeanWrapper 结构 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) BeanWrap ...
- 记录点复习题目和linux学习
哈希怎么底层.key放数组哪部分里面 HashMap实际上是一个“链表散列”的数据结构,即数组和链表的结合体. 开网页怎么获取页面 linux 看进程的cpu 和内存占用率 看哪个端口被占用 ...
- strace使用
统计信息 -c -- count time, calls, and errors for each syscall and report summary 输出结果到文件 -o 例如 strace ...
- centos nfs配置备忘
[需求]web应用需要部署在两台机器,图片目录共用,MySQL共用. [环境]Server: 192.168.168.10Client: 192.168.168.20 [配置步骤]1.在两台机器上安装 ...