CF463D Gargari and Permutations dp
给定 $n<=10$ 个 $1$~$n$ 的排列,求这些排列的 $LCS$.
考虑两个排列怎么做:以第一个序列为基准,将第二个序列的元素按照该元素在第一个序列中出现位置重新编号.
然后,求一个 $LIS$ 即可.
现在是多个串,不妨也按照这个方法来做:
以第一个串为基准,其余串重新编号成该元素在第一个串中的出现位置.
那么,如果一个序列是 $n$ 个序列的 $LCS$,则满足两个条件:
1: 是重现编号后的 $LCS$
2: 这个 $LCS$ 还需是一个 $LIS$.
多了第二个限制,问题就简单了:令 $f[i]$ 表示这些新编号串以 $i$ 数字结尾的满足两个条件的最长长度.
转移什么的就简单了~
code:
#include <bits/stdc++.h>
#define N 1004
using namespace std;
void setIO(string s)
{
string in=s+".in";
string out=s+".out";
freopen(in.c_str(),"r",stdin);
freopen(out.c_str(),"w",stdout);
}
struct node
{
int l,r;
}t[N];
int a[12][N],pos[N],f[N],L[12][N];
int main()
{
// setIO("seq");
int n,i,j,m,ans=1;
scanf("%d%d",&n,&m);
for(i=1;i<=m;++i) for(j=1;j<=n;++j) scanf("%d",&a[i][j]);
for(i=1;i<=n;++i) pos[a[1][i]]=i;
for(i=1;i<=m;++i) for(j=1;j<=n;++j) a[i][j]=pos[a[i][j]];
for(i=1;i<=m;++i) for(j=1;j<=n;++j) L[i][a[i][j]]=j;
f[1]=1;
for(i=2;i<=n;++i)
{
f[i]=1;
for(j=1;j<i;++j)
{
int flag=0;
for(int k=1;k<=10;++k) if(L[k][j]>L[k][i]) flag=1;
if(!flag) f[i]=max(f[i], f[j]+1), ans=max(ans, f[i]);
}
}
printf("%d\n",ans);
return 0;
}
CF463D Gargari and Permutations dp的更多相关文章
- codeforces 463D Gargari and Permutations(dp)
题目 参考网上的代码的... //要找到所有序列中的最长的公共子序列, //定义状态dp[i]为在第一个序列中前i个数字中的最长公共子序列的长度, //状态转移方程为dp[i]=max(dp[i],d ...
- CF 463D Gargari and Permutations [dp]
给出一个长为n的数列的k个排列(1 ≤ n ≤ 1000; 2 ≤ k ≤ 5).求这个k个数列的最长公共子序列的长度 dp[i]=max{dp[j]+1,where j<i 且j,i相应的字符 ...
- [CF463D]Gargari and Permutations
题目大意:给你$k(2\leqslant k\leqslant5)$个$1\sim n(n\leqslant10^3)$的排列,求它们的最长子序列 题解:将$k$个排列中每个元素的位置记录下来.如果是 ...
- 【题解】POJ2279 Mr.Young′s Picture Permutations dp
[题解]POJ2279 Mr.Young′s Picture Permutations dp 钦定从小往大放,然后直接dp. \(dp(t1,t2,t3,t4,t5)\)代表每一行多少人,判断边界就能 ...
- Codeforces Round #264 (Div. 2) D. Gargari and Permutations 多序列LIS+dp好题
http://codeforces.com/contest/463/problem/D 求k个序列的最长公共子序列. k<=5 肯定 不能直接LCS 网上题解全是图论解法...我就来个dp的解法 ...
- Codeforces 463D Gargari and Permutations:隐式图dp【多串LCS】
题目链接:http://codeforces.com/problemset/problem/463/D 题意: 给你k个1到n的排列,问你它们的LCS(最长公共子序列)是多长. 题解: 因为都是1到n ...
- Codeforces #264 (Div. 2) D. Gargari and Permutations
Gargari got bored to play with the bishops and now, after solving the problem about them, he is tryi ...
- CodeForces - 285E: Positions in Permutations(DP+组合数+容斥)
Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive in ...
- Codeforces 463D Gargari and Permutations
http://codeforces.com/problemset/problem/463/D 题意:给出k个排列,问这k个排列的最长公共子序列的长度. 思路:只考虑其中一个的dp:f[i]=max(f ...
随机推荐
- 笨办法学python 习题14 优化过 遇到问题的请看
print "\t what's you name?"user_name = raw_input('>') from sys import argvscript, = arg ...
- 【LEETCODE】49、数组分类,简单级别,题目:566,1089
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- spring boot + vue实现图片上传及展示
转载:https://blog.csdn.net/weixin_40337982/article/details/84031778 其中一部分对我很有帮助 转载记录下 首先,html页面: <! ...
- ABP 基于DDD的.NET开发框架 学习(七)继承不同的service直接调用api的区别
1.IApplicationService->IBaseService->具体IXXXService 具体XXXService->BaseService,具体IXXXService ...
- Java对list进行分页,subList()方法实现分页
/** * 自定义List分页工具 * @author hanwl */ public class PageUtil { /** * 开始分页 * @param list * @param pageN ...
- 用不上索引的sql
1.使用不等于操作符(<>, !=) 大于可以.小于可以,between and 也可以 2.使用 is null 或 is not null 任何包含null值的列都将不会被包含在索引中 ...
- echart 人头
<template> <div :class="className"> <div :id="id" class="spi ...
- JavaScript中匿名函数this指向问题
this对象是在运行时基于函数执行环境绑定的,在全局函数中,this=window,在函数被作为某个对象的方法调用时,this等于这个对象. 但是匿名函数的执行环境是全局性的,所以匿名函数的this指 ...
- npm 安装卸载模块 & ionic插件安装与卸载
npm安装模块 npm install xxx利用 npm 安装xxx模块到当前命令行所在目录 npm install -g xxx利用npm安装全局模块xxx 本地安装时将模块写入package.j ...
- 如何方便引用自己的python包
有时候想要把一些功能封装成函数然后包装到模块里面最后形成一个包,然后在notebook里面去引用它去处理自己的数据和分析一些有用的部分,比如自己在 之前用到的一个datascience模板就是这样组织 ...