Codeforces 463D
D. Gargari and Permutationstime limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
InputThe 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.
OutputPrint the length of the longest common subsequence.
Sample test(s)input4 3
1 4 2 3
4 1 2 3
1 2 4 3output3NoteThe 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的更多相关文章
- Codeforces 463D Gargari and Permutations
http://codeforces.com/problemset/problem/463/D 题意:给出k个排列,问这k个排列的最长公共子序列的长度. 思路:只考虑其中一个的dp:f[i]=max(f ...
- Codeforces 463D Gargari and Permutations(求k个序列的LCS)
题目链接:http://codeforces.com/problemset/problem/463/D 题目大意:给你k个序列(2=<k<=5),每个序列的长度为n(1<=n< ...
- Codeforces 463D Gargari and Permutations:隐式图dp【多串LCS】
题目链接:http://codeforces.com/problemset/problem/463/D 题意: 给你k个1到n的排列,问你它们的LCS(最长公共子序列)是多长. 题解: 因为都是1到n ...
- codeforces 463D Gargari and Permutations(dp)
题目 参考网上的代码的... //要找到所有序列中的最长的公共子序列, //定义状态dp[i]为在第一个序列中前i个数字中的最长公共子序列的长度, //状态转移方程为dp[i]=max(dp[i],d ...
- CodeForces 463D DP
Gargari got bored to play with the bishops and now, after solving the problem about them, he is tryi ...
- codeforces463D
Gargari and Permutations CodeForces - 463D Gargari got bored to play with the bishops and now, after ...
- codeforces的dp专题
1.(467C)http://codeforces.com/problemset/problem/467/C 题意:有一个长为n的序列,选取k个长度为m的子序列(子序列中不能有位置重复),求所取的k个 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
随机推荐
- [Ceoi2016|BZOJ4936] Match
哈希+分治+stack 题目: 给你一个由小写字母组成的字符串s,要你构造一个字典序最小的(认为左括号的字典序比右括号小)合法的括号 序列与这个字符串匹配,字符串和括号序列匹配定义为:首先长度必须相等 ...
- android 往sd卡中写入文件
在调用前需要判断是否有写入权限 Environment类提供了比较丰富的方法 static File getDataDirectory() 获得android data的目录. static File ...
- 筛法求欧拉函数(poj2478
求1-n的欧拉函数的值 #include <iostream> #include <cstdio> #include <queue> #include <al ...
- datetime与timestamp相互转换
select unix_timestamp('2019-12-05 12:26:35'); );
- 数组(Array)与 字符串(String)公用的属性与方法
数组与字符串都有很多方法,有一些方法是公用的,在这里就将数组与字符串公用的方法提取出来,方便大家的记忆 1. length 可通过str.length与arr.length分别取到字符串与数组的长度: ...
- BZOJ1912:[APIO2010]patrol巡逻
Description Input 第一行包含两个整数 n, K(1 ≤ K ≤ 2).接下来 n – 1行,每行两个整数 a, b, 表示村庄a与b之间有一条道路(1 ≤ a, b ≤ n). Ou ...
- 大文件传输 分片上传 上传id 分片号 授权给第三方上传
https://www.zhihu.com/question/39593108 作者:ZeroOne链接:https://www.zhihu.com/question/39593108/answer/ ...
- Java-Druid:目录
ylbtech-Java-Druid:目录 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:http://yl ...
- jdk11下载安装及环境变量配置
jdk11下载安装及环境变量配置 官网地址:https://www.oracle.com/technetwork/java/javase/downloads/jdk11-downloads-50666 ...
- java中生成不重复随机的数字
Java中产生随机数 1 . 调用java.lang下面Math类中的random()方法产生随机数 新建一个文件后缀名为java的文件,文件名取为MyRandom,该类中编写如下的代码: publi ...