uva103-Stacking_Boxes
Posted 啊嘞
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了uva103-Stacking_Boxes相关的知识,希望对你有一定的参考价值。
题目链接请戳 这里
解题思路
DAG上的动态规划。现对n个维度排序(感觉有维度的,先排序排序就行了
代码
#include<stdio.h> #include<string.h> #include<algorithm> #define N 40 using namespace std; bool G[N][N]; int box[N][N], dp[N]; int k, n; bool first; bool judge(int x, int y) { bool flag = true; for (int i = 0; i < n; i++) if (box[x][i] >= box[y][i]) flag = false; return flag; } int cal(int i) { int &ans = dp[i]; if (ans > 0) return ans; ans = 1; for (int j = 1; j <= k; j++) if (G[i][j]) ans = max(ans, cal(j) + 1); return ans; } void print_ans(int i) { if (first) { printf("%d", i); first = false; } else printf("% d", i); for (int j = 1; j <= k; j++) if (G[i][j] && dp[i] == dp[j] + 1) { print_ans(j); break; } } int main() { while (scanf("%d%d", &k, &n) != EOF) { memset(G, false, sizeof(G)); for (int i = 1; i <= k; i++) for (int j = 0; j < n; j++) scanf("%d", &box[i][j]); for (int i = 1; i <= k; i++) sort(box[i], box[i] + n); for (int i = 1; i <= k; i++) for (int j = 1; j <= k; j++) if (judge(i, j)) G[i][j] = true; memset(dp, 0, sizeof(dp)); for (int i = 1; i <= k; i++) cal(i); int max_index = 1; for (int i = 1; i <= k; i++) if (dp[i] > dp[max_index]) max_index = i; first = true; printf("%d\n", dp[max_index]); print_ans(max_index); printf("\n"); } return 0; }
以上是关于uva103-Stacking_Boxes的主要内容,如果未能解决你的问题,请参考以下文章