UVA 103 Stacking Boxes 套箱子 DAG最长路 dp记忆化搜索
题意:给出几个多维的箱子,如果箱子的每一边都小于另一个箱子的对应边,那就称这个箱子小于另一个箱子,然后要求能够套出的最多的箱子。
要注意的是关系图的构建,对箱子的边排序,如果分别都小于另一个箱子就说明是箱子小于,重载<即可。
然后就是正常的dp最长路的搜索了。
代码:
/*
* Author: illuz <iilluzen[at]gmail.com>
* Blog: http://blog.csdn.net/hcbbt
* File: uva103.cpp
* Create Date: 2013-09-12 19:32:36
* Descripton: dp
*/ #include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std; const int MAXN = 100; struct Box {
int dem;
int e[30];
void Sort() {
sort(e, e + dem);
}
bool operator < (const Box& a) const {
for (int i = 0; i < dem; i++)
if (e[i] >= a.e[i])
return false;
return true;
}
} b[MAXN];
int big[MAXN][MAXN], k, t;
int dp[MAXN]; int solve(int i) {
if (dp[i] > 0) return dp[i];
dp[i] = 1;
for (int j = 0; j < t; j++)
if (big[i][j])
dp[i] = max(dp[i], solve(j) + 1);
return dp[i];
} void output(int i) {
for (int j = 0; j < t; j++)
if (big[i][j] && dp[i] == dp[j] + 1) {
printf(" %d", j + 1);
output(j);
break;
}
} int main() {
while (scanf("%d%d", &t, &k) != EOF) {
for (int i = 0; i < t; i++) {
for (int j = 0; j < k; j++) {
b[i].dem = k;
scanf("%d", &b[i].e[j]);
}
b[i].Sort();
}
memset(big, 0, sizeof(big));
memset(dp, 0, sizeof(dp));
for (int i = 0; i < t; i++)
for (int j = 0; j < t; j++)
if (i != j && b[i] < b[j])
big[i][j] = 1;
for (int i = 0; i < t; i++)
solve(i);
int tt = 0;
for (int i = 0; i < t; i++)
if (dp[i] > dp[tt])
tt = i;
printf("%d\n", dp[tt]);
printf("%d", tt + 1);
output(tt);
printf("\n");
}
return 0;
}
UVA 103 Stacking Boxes 套箱子 DAG最长路 dp记忆化搜索的更多相关文章
- UVa 103 Stacking Boxes --- DAG上的动态规划
UVa 103 题目大意:给定n个箱子,每个箱子有m个维度, 一个箱子可以嵌套在另一个箱子中当且仅当该箱子的所有的维度大小全部小于另一个箱子的相应维度, (注意箱子可以旋转,即箱子维度可以互换),求最 ...
- 状压DP+记忆化搜索 UVA 1252 Twenty Questions
题目传送门 /* 题意:给出一系列的01字符串,问最少要问几个问题(列)能把它们区分出来 状态DP+记忆化搜索:dp[s1][s2]表示问题集合为s1.答案对错集合为s2时,还要问几次才能区分出来 若 ...
- uva 103 Stacking Boxes(DAG)
题目连接:103 - Stacking Boxes 题目大意:有n个w维立体, 输出立体互相嵌套的层数的最大值, 并输出嵌套方式, 可嵌套的要求是外层立体的w条边可以分别对应大于内层立体. 解题思路: ...
- UVA 103 Stacking Boxes (dp + DAG上的最长路径 + 记忆化搜索)
Stacking Boxes Background Some concepts in Mathematics and Computer Science are simple in one or t ...
- UVA 10003 Cutting Sticks 区间DP+记忆化搜索
UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的 ...
- UVa 10599【lis dp,记忆化搜索】
UVa 10599 题意: 给出r*c的网格,其中有些格子里面有垃圾,机器人从左上角移动到右下角,只能向右或向下移动.问机器人能清扫最多多少个含有垃圾的格子,有多少中方案,输出其中一种方案的格子编号. ...
- UVA 103 Stacking Boxes n维最长上升子序列
题目链接:UVA - 103 题意:现有k个箱子,每个箱子可以用n维向量表示.如果一个箱子的n维向量均比另一个箱子的n维向量大,那么它们可以套接在一起,每个箱子的n维向量可以互相交换值,如箱子(2,6 ...
- UVa 103 - Stacking Boxes(dp求解)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- uva 103 Stacking Boxes(最长上升子序列)
Description Stacking Boxes Background Some concepts in Mathematics and Computer Science are simp ...
随机推荐
- hdr_beg(host) 主机名开始
What does "acl cdn_name hdr_beg(host) -i foor.bar.com" mean in HAProxy's configuration? 在H ...
- hdu 1757 A Simple Math Problem(矩阵快速幂乘法)
Problem Description Lele now is thinking about a simple function f(x). If x < f(x) = x. If x > ...
- js算法
最近面试可能会问这些 1,插入排序 function sort(elements){ var res =[elements[0]]; for (var i = 0; i < elements.l ...
- win7运行sqlplus报错“SP2-1503: 无法初始化 Oracle 调用界面”
WIN7 64bit安装Oracle 10.2.0.1后,运行cmd-sqlplus / as sysdba会提示: C:\Users\Liu>sqlplus / as sysdba SP2-1 ...
- 运用Autoconf和Automake生成Makefile的学习之路
作为Linux下的程序开发人员,大家一定都遇到过Makefile,用make命令来编译自己写的程序确实是很方便.一般情况下,大家都是手工写一个简单Makefile,如果要想写出一个符合自由软件惯例的M ...
- oracle优化思考-双刃剑
oracle优化是一个双刃剑,特别注意这把剑用的场合:系统规划OLTP or OLAP 优化1:索引 在DML操作时.必须维护索引.假设大量的DML操作,想想看,IO是不是老高了? 索引长处:在非常多 ...
- Angular-UI-Router
原文 http://www.cnblogs.com/youngdze/p/4025538.html 主题 路由器JavaScript 路由 Route 为什么用 Route AJAX 请求不会留 ...
- jQuery.innerWidth() 函数详解
innerWidth()函数用于设置或返回当前匹配元素的内宽度. 内宽度包括元素的内边距(padding),但不包括外边距(margin).边框(border)等部分的高度.如下图: 如果你要获取 ...
- EXT.NET常用属性
Ext_数字输入框_Ext.form.NumberField: <mce:script type="text/javascript"><!-- /* Ext. ...
- L8_2
4.留下pid为12345的那个sh进程,杀死系统中所有其它sh进程 ps –ef|grep sh |awk ‘{if($2!=”12345”) {print “kill “$2}}’ >kil ...