题目链接

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. linux 解压 WinRAR 压缩文件

    1.Download rar for linux wget http://www.rarlab.com/rar/rarlinux-x64-5.5.b1.tar.gz 2.Configure rar t ...

  2. 非旋Treap及其可持久化

    平衡树这种东西,我只会splay.splay比较好理解,并且好打,操作方便. 我以前学过SBT,但并不是很理解,所以就忘了怎么打了. 许多用平衡树的问题其实可以用线段树来解决,我们真正打平衡树的时候一 ...

  3. DuiLib学习笔记1.编译运行demo

    c++中皮肤问题比较麻烦,MFC自带的太难用.DirectUI界面库就比较强大了,之前像skin++之类的基于DirectUI收费昂贵.DuiLib是基于DirectUI的界面库,可以将用户界面和处理 ...

  4. BZOJ 3090: Coci2009 [podjela] (树形背包)

    3090: Coci2009 [podjela] Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 45  Solved: 31[Submit][Statu ...

  5. python基础-基础知识考试_day5 (包括:函数_递归等知识)

    老男孩 Python 基础知识练习(三) 1.列举布尔值为 False 的值空,None,0, False, '', [], {}, () 2.写函数:根据范围获取其中 3 和 7 整除的所有数的和, ...

  6. PAT甲级——A1103 Integer Factorization

    The K−P factorization of a positive integer N is to write N as the sum of the P-th power of Kpositiv ...

  7. python实现批量修改服务器密码

    需求:机房.线上有多台主机,为了保障安全,需要定期修改密码.若手动修改,费时费力易出错. 程序应该满足如下需求 : 1.在现有的excel密码表格,在最后一个字段后面生成新的密码,另存为一个新的exc ...

  8. 03_Sklearn的安装

    1.Scikit-learn库介绍:包含许多知名的机器学习算法的实现,文档完善.容易上手,丰富的API. 2.安装:创建一个基于Python3的虚拟环境(可以在已有的虚拟环境中):mkvirtuale ...

  9. 新启vue_cli项目+引入Element

    [1]安装vue_cli vue init webpack 项目名字 [2]安装Element-UI cnpm install element-ui -S //写入dependencies cnpm ...

  10. tomcat结构图