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 ...
随机推荐
- 在js中使用Razor
@foreach (var tem in Model) { <text> time.push("@tem.CreateTime.ToString("G")&q ...
- gnome3 修改桌面背景图片模式
修改背景图片,可以在 桌面右键 选择"修改壁纸",选择"background"(背景),这里没有设定背景图片模式. 可以在 “应用程序”->"工 ...
- css实现字母或数字强制换行
//换行white-space:normal;word-break:break-all;word-wrap: break-word; 相关属性white-space: normal|pre|nowra ...
- 【JZOJ1259】牛棚安排
description Farmer John的N(1<=N<=1000)头奶牛分别居住在农场所拥有的B(1<=B<=20)个牛棚的某一个里.有些奶牛很喜欢她们当前住的牛棚,而 ...
- 【转载】linux进程及进程控制
Linux进程控制 程序是一组可执行的静态指令集,而进程(process)是一个执行中的程序实例.利用分时技术,在Linux操作系统上同时可以运行多个进程.分时技术的基本原理是把CPU的运行时间划 ...
- echarts高级
常用,待续... ♣tooltip自动轮播 ♣ 实现数据自动轮播 原理:其实就是timeline,获取某几段(时间)的数据,然后隐藏timeline ♣ legend自动轮播 ♣ 左侧多字出省略号 f ...
- SQLServer索引的四个高级特性
一Index Building Filter索引创建时过滤 二Index Include Column索引包含列 三聚集索引Cluster Index 四VIEW INDEX视图索引 SQLSer ...
- mac下安装Python的工具包pip
1. 在终端下输入 sudo easy_install pip password:输入电脑密码 Finished processing dependencies for pip 表示安装完成 boe ...
- 2018CCPC吉林赛区 | 部分题解 (HDU6555 HDU6556 HDU6559 HDU6561)
// 杭电上的重现赛:http://acm.hdu.edu.cn/contests/contest_show.php?cid=867 // 杭电6555~6566可交题 A - The Fool 题目 ...
- selenium简单应用
文章引用自:https://wenku.baidu.com/view/d5c296c75727a5e9846a6182.html 例子: