codeforces463D
Gargari and Permutations
Gargari got bored to play with the bishops and now, after solving the problem about them, he is trying to do math homework. In a math book he have found k permutations. Each of them consists of numbers 1, 2, ..., n in some order. Now he should find the length of the longest common subsequence of these permutations. Can you help Gargari?
You can read about longest common subsequence there: https://en.wikipedia.org/wiki/Longest_common_subsequence_problem
Input
The first line contains two integers n and k (1 ≤ n ≤ 1000; 2 ≤ k ≤ 5). Each of the next k lines contains integers 1, 2, ..., n in some order — description of the current permutation.
Output
Print the length of the longest common subsequence.
Examples
4 3
1 4 2 3
4 1 2 3
1 2 4 3
3
Note
The answer for the first test sample is subsequence [1, 2, 3].
sol:求m个序列的lcs,似乎看上去很难得样子,实际上想到dp状态就不难了,dp[i]表示以数字 i 结尾的m个序列的lcs长度,然后判断一下是否所有数字 j 都在 i 之前就可以转移了
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int n,m,a[][N],Pos[][N];
int dp[N];
int main()
{
int i,j,k;
R(n); R(m);
for(i=;i<=m;i++)
{
for(j=;j<=n;j++)
{
R(a[i][j]); Pos[i][a[i][j]]=j;
}
}
dp[a[][]]=;
for(i=;i<=n;i++)
{
for(j=;j<i;j++)
{
bool Flag=;
for(k=;k<=m&&Flag;k++) if(Pos[k][a[][j]]>Pos[k][a[][i]]) Flag=;
if(Flag) dp[a[][i]]=max(dp[a[][i]],dp[a[][j]]+);
}
if(!dp[a[][i]]) dp[a[][i]]=;
}
int ans=;
for(i=;i<=n;i++) ans=max(ans,dp[i]);
Wl(ans);
return ;
}
/*
Input
4 3
1 4 2 3
4 1 2 3
1 2 4 3
Output
3
*/
codeforces463D的更多相关文章
随机推荐
- SLAM+语音机器人DIY系列:(二)ROS入门——1.ROS是什么
摘要 ROS机器人操作系统在机器人应用领域很流行,依托代码开源和模块间协作等特性,给机器人开发者带来了很大的方便.我们的机器人“miiboo”中的大部分程序也采用ROS进行开发,所以本文就重点对ROS ...
- MySQL 笔记整理(10) --MySQL为什么有时会选错索引?
笔记记录自林晓斌(丁奇)老师的<MySQL实战45讲> (本篇内图片均来自丁奇老师的讲解,如有侵权,请联系我删除) 10) --MySQL为什么有时会选错索引? MySQL中的一张表上可以 ...
- LINQ之道
提到LINQ首先我们要了解什么是委托:委托是一种引用方法的类型.一旦为委托分配了方法,委托将与该方法具有完全相同的行为.也就是说当你委托给一个人办一件事的时候,他就拥有这个能力去实现这件事,同样委托也 ...
- 详解RPC远程调用和消息队列MQ的区别
PC(Remote Procedure Call)远程过程调用,主要解决远程通信间的问题,不需要了解底层网络的通信机制. RPC框架 知名度较高的有Thrift(FB的).dubbo(阿里的). RP ...
- OO第二次博客作业——电梯调度
OO第二次博客作业——电梯调度 前言 最近三周,OO课程进入多线程学习阶段,主要通过三次电梯调度作业来学习.从单部电梯的傻瓜式调度到有性能要求的调度到多部电梯的调度,难度逐渐提升,对同学们的要求逐渐变 ...
- 2018-07-10 为Chrome和火狐浏览器编写扩展
由于扩展标准的逐渐一致, 现在同一扩展代码库已经有可能同时用于Chrome和火狐. 下面是一个简单的工具栏按钮和弹窗(尚无任何实际功能): 代码库地址: nobodxbodon/suan1 所有代码: ...
- Umi+Dva搭建Cesium 3D开发环境
umi,中文可发音为乌米,是一个可插拔的企业级 react 应用框架,是蚂蚁金服的底层前端框架,已直接或间接地服务了 600+ 应用,包括 java.node.H5 无线.离线(Hybrid)应用.纯 ...
- linux 大冒险
本来想搞一个nas系统,结果上来linux的贼船. 本来是看上了deepin深度linux,结果看到排名第一的manjaro 就忍不住手.通过hyper-v虚拟机安装,发现这个所谓的第一不知道第一在哪 ...
- base64图片存储
将图片转换为Base64编码,可以让你很方便地在没有上传文件的条件下将图片插入其它的网页.编辑器中. 这对于一些小的图片是极为方便的,因为你不需要再去寻找一个保存图片的地方. Base64编码在ora ...
- Git - git tag - 查看当前分支 tag 版本&说明
索引: 目录索引 参看代码 GitHub: git.txt 一.示例: git tag -l -n 二.说明: 1."tag" 部分 tag 代表的是标签动作,可以带参数 ,也可以 ...