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 ...
随机推荐
- 关于python程序在VS code中运行时提示文件无法找到的报错
经过测试,在设置文件夹目录时,可以找到当前目录下的htm文件,采用with open()语句可以正常执行程序,如下图. 而当未设置当前目录,直接用vscode执行该程序时,就会报错文件无法找到File ...
- [JZOJ5233] 【GDOI模拟8.5】概率博弈
题目 题目大意 给你一棵树,这棵树上的所有叶子节点的权值是随机的排列. 两个人博弈,从根开始,轮流往下走. 先手希望权值最大,后手希望权值最小. 问期望的最终结果. 思考历程 这场比赛实在是太丧心病狂 ...
- JUC 一 Callable
java.util.concurrent.Callable是一个泛型接口,只有一个call()方法 Callable和Runnable的区别 Callable使用call()方法,Runnable使用 ...
- duilib教程之duilib入门简明教程1.前言
关于duilib的介绍就不多讲了,一来不熟,二来小伙伴们想必已经对比了多个界面库,也无需赘述.下面进入正题: 不看广告看疗效! 已有众多知名公司采用duilib做为界面库,如华为网盘.PPS(P ...
- python相关软件安装流程图解————————pycharm安装——————pycharm-professional-2018.3.1
https://www.jetbrains.com/pycharm/download/#section=windows http://www.cnblogs.com/ceshi2016/p/91129 ...
- mysql 主从,双主同步
1.创建用户并设置远程访问授权 1). A上添加: //ip地址为B的ip地址,用于B访问 ' with grant option; 2). B上添加://ip地址为A的ip地址,用于A访问 ' wi ...
- 47 Majority Element II
原题网址; https://www.lintcode.com/problem/majority-element-ii/ 描述 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三 ...
- 用Spire.PDF提取PDF里的PNG图片
用Nuget抓取类库,FreeSpire.PDF就可以 代码如下 , 亲测可以抓取PNG图形,即使原图是JPG,也会存成PNG格式输出: //加载PDF文档 PdfDocument doc = new ...
- flock文件锁的学习和应用
flock文件锁 学习与应用 2016-9-20 作用: 可以使用flock文件锁,避免指定命令的同时执行.(实现任务锁定,解决冲突) 用法: # flock -xn /opt/lock_file ...
- 06_jQuery对象初识(四)文档处理
1. 案例:在ul中添加li标签. append在最后添加 prepend在最前面添加 <ul id="ul"> <li>1</li> < ...