Codeforces #264 (Div. 2) D. Gargari and Permutations(动态规划:简单)
来源:互联网 发布:淘宝一键抢拍神器 编辑:程序博客网 时间:2024/06/02 04:02
这个题的状态转移方程本身还是很简单的,但是不是很容易想到
因为题目标明了每行数据都是1-n的排列
所以我们可以用dp[i]表示几组数据下标1-i范围的LCS
则很容易得到状态方程dp[j] = max(dp[i]+1, dp[j])(当前仅当每组数据中i都在j前面成立)
代码如下:
#include <bits/stdc++.h>#define MAXN 1010using namespace std;int n, k;int a[MAXN][MAXN], b[MAXN][MAXN], dp[MAXN];bool judge(int x, int y) { for(int i=2; i<=k; ++i) { //不用从1开始是因为在调用该函数的循环中已经可以确定y在x后出现 if(b[i][x] > b[i][y] ) return false; } return true;}int main(void) { scanf("%d%d", &n, &k); for(int i=1; i<=k; ++i) for(int j=1; j<=n; ++j) { scanf("%d", &a[i][j]); b[i][a[i][j]] = j; } for(int i=1; i<=n; ++i) dp[i] = 1; int ans = 0; for(int i=1; i<=n; ++i) { for(int j=i+1; j<=n; ++j) { if(judge(a[1][i], a[1][j])) { dp[j] = max(dp[i]+1, dp[j]); //printf("dp[%d] = %d\n", j, dp[j]); } } } for(int i=1; i<=n; ++i) ans = max(ans, dp[i]); printf("%d\n", ans); return 0;}
0 0
- 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 #264 div.2 D. Gargari and Permutations
- Codeforces #264(div 2)D.Gargari and Permutations
- Codeforces 264 (Div. 2) D. Gargari and Permutations
- Codeforces #264 (Div. 2) D. Gargari and Permutations(DAG求最长路)
- Codeforces Round #264 (Div. 2) D. Gargari and Permutations 多序列LIS+dp好题
- 【CODEFORCES】 D. Gargari and Permutations
- Codeforces 463D Gargari and Permutations(BFS)
- Codeforces 463D. Gargari and Permutations【DP】
- codeforces 463D Gargari and Permutations
- 【CodeForces】463D Gargari and Permutations DP
- Codeforces 463D Gargari and Permutations
- Codeforces 463D Gargari and Permutations(BFS)
- codeforces 463D D. Gargari and Permutations(dp)
- Codeforces 463D Gargari and Permutations DP(LCS变形)
- Codeforces Round #264 (Div. 2) C Gargari and Bishops 【暴力】
- iOS设计模式之Target-Action
- [LeetCode]-Triangle 求三角形中从顶到底最短距离
- 步科ET070不能启动
- Facebook's std::vector optimization
- 【Android】gradle使用过程的问题解决汇总
- Codeforces #264 (Div. 2) D. Gargari and Permutations(动态规划:简单)
- IE6兼容性问题及IE6常见bug详细汇总
- Android开发————布局参数的详解
- java.lang.VerifyError解决方案
- .NET应用架构设计—适当使用活动记录模式代替领域模型模式
- 数据库学习--mysql 分页查询语句
- [企业路由器] IPSec VPN设置指南
- JDBC中Statement和PrepareStatement的区别及特性
- audio的应用和常见属性