Codeforces Round #264 (Div. 2) D. Gargari and Permutations 多序列LIS+dp好题
2014-10-24 22:42
405 查看
http://codeforces.com/contest/463/problem/D
求k个序列的最长公共子序列。
k<=5
肯定 不能直接LCS
网上题解全是图论解法...我就来个dp的解法吧
首先,让我们创建的位置阵列的每个序列pos[k]
。在这个数组,我们存储这n个数字(从1到n)在这k个序列中各个位置,然后按数大小排序。
DP [I]标记的最大长度共同序列,我们可以通过使用第一个序列的第i个元素生成。为此,我们遍历所有可能以前的编号(J),看看我们是否能延长DP [J]到DP [I]。
对于给定的J<I,只有在所有的序列中J的位置前与I,才能更新DP[I] = max(DP[J] + 1).
求k个序列的最长公共子序列。
k<=5
肯定 不能直接LCS
网上题解全是图论解法...我就来个dp的解法吧
首先,让我们创建的位置阵列的每个序列pos[k]
。在这个数组,我们存储这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 #264 (Div. 2) D. Gargari and Permutations(DAG求最长路)
- Codeforces Round #371 (Div. 2) E. Sonya and Problem Wihtout a Legend (DP/LIS变形)
- Codeforces #264 (Div. 2) D. Gargari and Permutations(动态规划:简单)
- Codeforces #264(div 2)D.Gargari and Permutations
- Codeforces Round #371 (Div. 2)E. Sonya and Problem Wihtout a Legend[DP 离散化 LIS相关]
- Codeforces #264 (Div. 2) D. Gargari and Permutations
- codeforces 463D D. Gargari and Permutations(dp)
- codeforce 263 div2D Gargari and Permutations
- Codeforces Round #371 (Div. 2) E 【DP+离散化 LIS 】用最小代价把序列变成严格递增序列
- Codeforces #264 (Div. 2) D. Gargari and Permutations
- Codeforces #264 (Div. 2) D. Gargari and Permutations
- Codeforces 264 (Div. 2) D. Gargari and Permutations
- Codeforces 463D. Gargari and Permutations【DP】
- HDU 1069 Monkey and Banana DP LIS
- Codeforces Round #263 (Div. 2) D. Appleman and Tree(树形DP)
- Codeforces Round #261 (Div. 2)(树状数组,dp,dfs构造序列)
- Codeforces Round 264(div2) C. Gargari and Bishops
- Codeforces Round #263 Div.1 B Appleman and Tree --树形DP【转】
- Codeforces Round #235 (Div. 2) D Roman and Numbers(状态DP)