Codeforces Round #264 (Div. 2) D. Gargari and Permutations 多序列LIS+dp好题
http://codeforces.com/contest/463/problem/D
求k个序列的最长公共子序列。
k<=5
肯定 不能直接LCS
网上题解全是图论解法...我就来个dp的解法吧
首先,让我们创建的位置阵列的每个序列pos[k][N]。在这个数组,我们存储这n个数字(从1到n)在这k个序列中各个位置,然后按数大小排序。
DP [I]标记的最大长度共同序列,我们可以通过使用第一个序列的第i个元素生成。为此,我们遍历所有可能以前的编号(J),看看我们是否能延长DP [J]到DP [I]。
对于给定的J<I,只有在所有的序列中J的位置前与I,才能更新DP[I] = max(DP[J] + 1).
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include<set>
#include <iostream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define clr0(x) memset(x,0,sizeof(x))
typedef long long LL;
vector <int> pos[1005];
int dp[1005];
int main()
{
int n,k,x;
RD2(n,k);
for(int i = 0;i < k;++i){
for(int j = 1;j <= n;++j){
RD(x);
pos[x].push_back(j);
}
}
sort(pos+1,pos+n+1);
int ans = 1;
for(int i = 1;i <= n;++i)
dp[i] = 1;
for(int i = 2;i <= n;++i){
for(int j = 1;j < i;++j){
bool ok = true;
for(int l = 0;l < k;++l){
if(pos[j][l] > pos[i][l]){
ok = false;
break;
}
}
if(ok){
dp[i] = max(dp[j]+1,dp[i]);
}
}
ans = max(ans,dp[i]);
}
printf("%d\n",ans);
return 0;
}
Codeforces Round #264 (Div. 2) D. Gargari and Permutations 多序列LIS+dp好题的更多相关文章
- Codeforces Round #264 (Div. 2) C. Gargari and Bishops 主教攻击
http://codeforces.com/contest/463/problem/C 在一个n∗n的国际象棋的棋盘上放两个主教,要求不能有位置同时被两个主教攻击到,然后被一个主教攻击到的位置上获得得 ...
- Codeforces Round #264 (Div. 2) C Gargari and Bishops 【暴力】
称号: 意甲冠军:给定一个矩阵,每格我们有一个数,然后把两个大象,我希望能够吃的对角线上的所有数字.我问两个最大的大象可以吃值. 分析:这种想法是暴力的主题,计算出每一格放象的话能得到多少钱,然后求出 ...
- Codeforces Round #485 (Div. 2) E. Petr and Permutations
Codeforces Round #485 (Div. 2) E. Petr and Permutations 题目连接: http://codeforces.com/contest/987/prob ...
- Codeforces Round #539 (Div. 2) - C. Sasha and a Bit of Relax(思维题)
Problem Codeforces Round #539 (Div. 2) - C. Sasha and a Bit of Relax Time Limit: 2000 mSec Problem ...
- Codeforces Round #433 (Div. 2)【A、B、C、D题】
题目链接:Codeforces Round #433 (Div. 2) codeforces 854 A. Fraction[水] 题意:已知分子与分母的和,求分子小于分母的 最大的最简分数. #in ...
- Codeforces Round #264 (Div. 2)
http://codeforces.com/contest/463 这场是我人生第一场cf啊.. 悲剧处处是啊. 首先,看不懂题,完全理解不了啊.都是wa了好几次才过的 所以a和b这两sb题我做了1个 ...
- Codeforces Round #264 (Div. 2) C
题目: C. Gargari and Bishops time limit per test 3 seconds memory limit per test 256 megabytes input s ...
- Codeforces Round #264 (Div. 2) E. Caisa and Tree 树上操作暴力
http://codeforces.com/contest/463/problem/E 给出一个总节点数量为n的树,每个节点有权值,进行q次操作,每次操作有两种选项: 1. 询问节点v到root之间的 ...
- 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 ...
随机推荐
- 8M - 三角形
用N个三角形最多可以把平面分成几个区域? Input 输入数据的第一行是一个正整数T(1<=T<=10000),表示测试数据的数量.然后是T组测试数据,每组测试数据只包含一个正整数N(1 ...
- (转)数组使用contains
数组使用contains 今天发现一个怪问题,同样是.net3.5环境下的两个项目,一个里支持arr.contains("1"),一个就不支持,代码完全相同也不行.有时在不支持项目 ...
- PAT 1087 有多少不同的值(20)(STL-set代码)
1087 有多少不同的值(20 分) 当自然数 n 依次取 1.2.3.--.N 时,算式 ⌊n/2⌋+⌊n/3⌋+⌊n/5⌋ 有多少个不同的值?(注:⌊x⌋ 为取整函数,表示不超过 x 的最大自然数 ...
- PAT 1061 判断题(15)(代码)
1061 判断题(15 分) 判断题的评判很简单,本题就要求你写个简单的程序帮助老师判题并统计学生们判断题的得分. 输入格式: 输入在第一行给出两个不超过 100 的正整数 N 和 M,分别是学生人数 ...
- String 练习
package com.hanqi; import java.util.Random; public class Text { public static void main(String[] arg ...
- gulp ( http://markpop.github.io/2014/09/17/Gulp入门教程 )
前言 最近流行前端构建工具,苦于之前使用Grunt,代码很难阅读,现在出了Gulp,真是摆脱了痛苦.发现了一篇很好的Gulp英文教程,整理翻译给大家看看. 为什么使用Gulp Gulp基于Node.j ...
- Qt5+VS2010的安装及使用
在我的博客<Win7下Qt5的安装及使用>中讲解了win7下Qt5+MinGW的安装及使用,本节再讲解win7下Qt5+VS2010的安装及使用.利用Qt5+MinGW开发应用程序比较麻烦 ...
- c#比delphi先进的地方
下面是一个例子: using System;namespace HelloWorld{ class Hello{ private static string DateDiff( ...
- MySQL8.0的安装与配置(Windows 10)
MySQL的下载 进入MySQL官网 https://www.mysql.com/ 选择 DOWNLOAD 到页面底部找到 MySQL Community Edition (GPL) 找到MySQL ...
- DNSlog盲注
前言 在渗透测试中,经常遇到如下情景: 1,sql盲注 2,blind型ssrf以及xxe 3,无回显命令执行漏洞 ... dnslog盲注原理 开放的DNSlog平台: http://ceye.io ...