题目链接

D. Gargari and Permutations
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Sample test(s)
input
4 3
1 4 2 3
4 1 2 3
1 2 4 3
output
3
Note

The answer for the first test sample is subsequence [1, 2, 3].

题意:给出k个n的全排列,求k个串的最长公共子序列(1<= k <= 5).

刚拿到题感觉是个dp,但是又没有明确的思路,yy了两发,发现不对,就没有做了。

今天才知道这个题可以通过建图然后得到若干个DAG,然后求最长路。

让我想起了某此CF的C题。那题可以转化为求MST,万万没想到。

今天记下,以后应多往图这方面想,根据题意是否能构造出图来,然后我只想说题水人更水。

本题可以这样来构图,如果在每个序列中,i都在j前面,那么可以从i向j连一条边。

最后求以每个点为根的最长路,取最大值即可。

Accepted Code:

 /*************************************************************************
> File Name: 463D.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年08月31日 星期日 22时02分01秒
> Propose:
************************************************************************/ #include <cmath>
#include <string>
#include <cstdio>
#include <vector>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
/*Let's fight!!!*/ #define rep(i, n) for (int i = (0); i < (n); i++)
#define FOR(i, n) for (int i = (1); i <= (n); i++)
int n, k;
int a[][], dp[], pos[][];
vector<int> G[];
bool vis[]; bool check(int x, int y) {
rep (i, k) if (pos[i][x] > pos[i][y]) return false;
return true;
} void dfs(int u) {
if (vis[u]) return;
vis[u] = true;
int sz = (int)G[u].size();
rep (i, sz) {
int v = G[u][i];
dfs(v);
dp[u] = max(dp[u], dp[v]);
}
dp[u]++;
} int main(void) {
cin >> n >> k;
int a;
rep (i, k) rep(j, n) cin >> a, pos[i][a] = j;
FOR (i, n) FOR (j, n) if (i != j && check(i, j)) G[i].push_back(j); // FOR (i, n) rep (j, G[i].size()) cerr << i << "-->" << G[i][j] << endl; memset(vis, false, sizeof(vis));
FOR (i, n) if (!vis[i]) dfs(i); int ans = ;
FOR (i, n) ans = max(ans, dp[i]);
cout << ans << endl; return ;
}

 

Codeforces 463D的更多相关文章

  1. Codeforces 463D Gargari and Permutations

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

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

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

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

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

  4. codeforces 463D Gargari and Permutations(dp)

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

  5. CodeForces 463D DP

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

  6. codeforces463D

    Gargari and Permutations CodeForces - 463D Gargari got bored to play with the bishops and now, after ...

  7. codeforces的dp专题

    1.(467C)http://codeforces.com/problemset/problem/467/C 题意:有一个长为n的序列,选取k个长度为m的子序列(子序列中不能有位置重复),求所取的k个 ...

  8. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  9. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

随机推荐

  1. ubuntu下apache服务器操作方法小结,具有参考借鉴价值

    这篇文章主要介绍了ubuntu下apache服务器操作方法小结,非常不错,具有参考借鉴价值,需要的朋友可以参考下(http://www.0831jl.com)Linux系统为Ubuntu 一.Star ...

  2. 在python3中的编码

    在python3中的编码 #_author:Administrator#date:2019/10/29import sysprint(sys.getdefaultencoding())#utf-8 打 ...

  3. Tensortflow安装

    1.  CMD里面 pip install --upgrade --ignore-installed tensorflow

  4. LUOGU P4777 【模板】扩展中国剩余定理(EXCRT)

    传送门 解题思路 扩展 $crt​$,就是中国剩余定理在模数不互质的情况下,首先对于方程 ​     $\begin{cases} x\equiv a_1\mod m_1\\x\equiv a_2\m ...

  5. QQ交流群

  6. Microsoft store打不开,解决办法

    1.打开电脑,点击左下角的图标开始,然后找到设置选项,也可以直接使用快捷键win+i: 2.在弹出的新页面中有很多选项功能,找到并且点击”网络和Internet“选项: 3.查看网络连接方式,如果是宽 ...

  7. Spring SpringMVC SpringBoot SpringCloud 注解整理大全

    Spring SpringMVC SpringBoot SpringCloud 注解整理 才开的博客所以放了一篇以前整理的文档,如果有需要添加修改的地方欢迎指正,我会修改的φ(๑˃∀˂๑)♪ Spri ...

  8. Python学习之while练习--九九乘法表

    效果如下: 实现代码; m = 1n = 1while(m<10): while(n<=m): print(n,"*",m,"=",m*n,end ...

  9. 重装一次CM的坑爹记录

    今天同事要对测试环境进行降级(测试高于生产所以要求降级),自己不经常搞运维,但是无奈测试环境没运维管理只能自己上了. 流程和遇到问题按数字表示. 1.重装CM(clouder manager)这个过程 ...

  10. 论文翻译——R-CNN(目标检测开山之作)

    R-CNN论文翻译 <Rich feature hierarchies for accurate object detection and semantic segmentation> 用 ...