题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=39

 Stacking Boxes 

Background

Some concepts in Mathematics and Computer Science are simple in one or two dimensions but become more complex when extended to arbitrary dimensions. Consider solving differential equations in several dimensions and analyzing the topology of an n-dimensional hypercube. The former is much more complicated than its one dimensional relative while the latter bears a remarkable resemblance to its ``lower-class'' cousin.

The Problem

Consider an n-dimensional ``box'' given by its dimensions. In two dimensions the box (2,3) might represent a box with length 2 units and width 3 units. In three dimensions the box (4,8,9) can represent a box  (length, width, and height). In 6 dimensions it is, perhaps, unclear what the box (4,5,6,7,8,9) represents; but we can analyze properties of the box such as the sum of its dimensions.

In this problem you will analyze a property of a group of n-dimensional boxes. You are to determine the longest nesting string of boxes, that is a sequence of boxes  such that each box  nests in box  (  .

A box D = (  ) nests in a box E = (  ) if there is some rearrangement of the  such that when rearranged each dimension is less than the corresponding dimension in box E. This loosely corresponds to turning box D to see if it will fit in box E. However, since any rearrangement suffices, box D can be contorted, not just turned (see examples below).

For example, the box D = (2,6) nests in the box E = (7,3) since D can be rearranged as (6,2) so that each dimension is less than the corresponding dimension in E. The box D = (9,5,7,3) does NOT nest in the box E = (2,10,6,8) since no rearrangement of D results in a box that satisfies the nesting property, but F = (9,5,7,1) does nest in box E since F can be rearranged as (1,9,5,7) which nests in E.

Formally, we define nesting as follows: box D = (  ) nests in box E = (  ) if there is a permutation  of  such that (  ) ``fits'' in (  ) i.e., if  for all .

The Input

The input consists of a series of box sequences. Each box sequence begins with a line consisting of the the number of boxes k in the sequence followed by the dimensionality of the boxes, n (on the same line.)

This line is followed by k lines, one line per box with the n measurements of each box on one line separated by one or more spaces. The  line in the sequence (  ) gives the measurements for the  box.

There may be several box sequences in the input file. Your program should process all of them and determine, for each sequence, which of the k boxes determine the longest nesting string and the length of that nesting string (the number of boxes in the string).

In this problem the maximum dimensionality is 10 and the minimum dimensionality is 1. The maximum number of boxes in a sequence is 30.

The Output

For each box sequence in the input file, output the length of the longest nesting string on one line followed on the next line by a list of the boxes that comprise this string in order. The ``smallest'' or ``innermost'' box of the nesting string should be listed first, the next box (if there is one) should be listed second, etc.

The boxes should be numbered according to the order in which they appeared in the input file (first box is box 1, etc.).

If there is more than one longest nesting string then any one of them can be output.

Sample Input

5 2
3 7
8 10
5 2
9 11
21 18
8 6
5 2 20 1 30 10
23 15 7 9 11 3
40 50 34 24 14 4
9 10 11 12 13 14
31 4 18 8 27 17
44 32 13 19 41 19
1 2 3 4 5 6
80 37 47 18 21 9

Sample Output

5
3 1 2 4 5
4
7 2 5 6 解题思路:
题目意思:给定n个m维的矩形,问我们能够嵌套的矩形最多有几个,输出个数和嵌套的矩形编号。
代码:
 #include<bits/stdc++.h>
#define inf 0x7fffffff
using namespace std;
typedef long long LL; int k,n;
int dp[],pre[];
struct node
{
int an[];
int id;
friend bool operator < (node a,node b)
{
for (int i= ;i<n ;i++)
{
if (a.an[i] != b.an[i]) return a.an[i] < b.an[i];
}
}
}arr[]; void printOut(int u)
{
if (pre[u]!=-) printOut(pre[u]);
if (pre[u]==-) printf("%d",arr[u].id+ );
else printf(" %d",arr[u].id+ );
} int main()
{
while (scanf("%d%d",&k,&n)!=EOF)
{
memset(dp,,sizeof(dp));
memset(pre,-,sizeof(pre));
for (int i= ;i<k ;i++)
{
for (int j= ;j<n ;j++)
scanf("%d",&arr[i].an[j]);
arr[i].id=i;
sort(arr[i].an,arr[i].an+n);
}
sort(arr,arr+k);
for (int i= ;i<k ;i++)
{
int temp=;
for (int j= ;j<i ;j++)
{
int flag=;
for (int u= ;u<n ;u++)
if (arr[i].an[u]<=arr[j].an[u]) {flag=;break; }
if (!flag && dp[j]>temp)
{
temp=dp[j];
pre[i]=j;
}
}
dp[i]=temp+;
}
int maxlen=-,num=;
for (int i= ;i<k ;i++)
{
if (dp[i]>maxlen)
{
maxlen=dp[i];
num=i;
}
}
printf("%d\n",maxlen);
printOut(num);
printf("\n");
}
return ;
}
 

UVa 103 - Stacking Boxes(dp求解)的更多相关文章

  1. UVA 103 Stacking Boxes (dp + DAG上的最长路径 + 记忆化搜索)

     Stacking Boxes  Background Some concepts in Mathematics and Computer Science are simple in one or t ...

  2. UVa 103 Stacking Boxes --- DAG上的动态规划

    UVa 103 题目大意:给定n个箱子,每个箱子有m个维度, 一个箱子可以嵌套在另一个箱子中当且仅当该箱子的所有的维度大小全部小于另一个箱子的相应维度, (注意箱子可以旋转,即箱子维度可以互换),求最 ...

  3. uva 103 Stacking Boxes(DAG)

    题目连接:103 - Stacking Boxes 题目大意:有n个w维立体, 输出立体互相嵌套的层数的最大值, 并输出嵌套方式, 可嵌套的要求是外层立体的w条边可以分别对应大于内层立体. 解题思路: ...

  4. UVa 103 - Stacking Boxes (LIS,打印路径)

    链接:UVa 103 题意:给n维图形,它们的边长是{d1,d2,d3...dn},  对于两个n维图形,求满足当中一个的全部边长 依照随意顺序都一一相应小于还有一个的边长,这种最长序列的个数,而且打 ...

  5. UVA 103 Stacking Boxes n维最长上升子序列

    题目链接:UVA - 103 题意:现有k个箱子,每个箱子可以用n维向量表示.如果一个箱子的n维向量均比另一个箱子的n维向量大,那么它们可以套接在一起,每个箱子的n维向量可以互相交换值,如箱子(2,6 ...

  6. uva 103 Stacking Boxes(最长上升子序列)

    Description    Stacking Boxes  Background Some concepts in Mathematics and Computer Science are simp ...

  7. UVA 103 Stacking Boxes 套箱子 DAG最长路 dp记忆化搜索

    题意:给出几个多维的箱子,如果箱子的每一边都小于另一个箱子的对应边,那就称这个箱子小于另一个箱子,然后要求能够套出的最多的箱子. 要注意的是关系图的构建,对箱子的边排序,如果分别都小于另一个箱子就说明 ...

  8. UVa 103 - Stacking Boxes

    题目大意:矩阵嵌套,不过维数是多维的.有两个个k维的盒子A(a1, a1...ak), B(b1, b2...bk),若能找到(a1...ak)的一个排列使得ai < bi,则盒子A可嵌套在盒子 ...

  9. UVA 103 Stacking Boxes --LIS

    实际上是一个扩展维度的矩形嵌套问题. 一个物体能嵌入另一个物体中,当且仅当这个物体的所有维度的长度都小于另外一个(本题是小于等于),又因为可以旋转等变换,所以干脆将每个箱子的边从小到大排序,以便于判断 ...

随机推荐

  1. [OpenCV] Basic data types - Matrix

    http://docs.opencv.org/2.4.13/ Basis 矩形 "modules/core/src/drawing.cpp" CV_IMPL void cvRect ...

  2. [git]修改commit

    git commit --amend 修改上一个的commit信息. git reset commit_id 修改commit,同时改变commit历史,可用于合并commit. git revert ...

  3. 对于Discuz!NT不允许新用户注册的解决办法

    客户论坛用的是Discuz!NT,但是用户注册总是提示不允许新用户注册,对于这个问题,网上好多说的是管理员登录后台,在"用户与访问控制"里将允许新用户注册改为"是&quo ...

  4. javascript中的数组扩展(一)

     javascript中的数组扩展(一) 随着学习的深入,发现需要学习的关于数组的内容也越来越多,后面将会慢慢归纳,有的是对前面的强化,有些则是关于前面的补充. 一.数组的本质    数组是按照次序排 ...

  5. 使用VS GDB扩充套件在VS上远端侦错Linux上的C/C++程序

    在 Linux 上开发 C/C++ 程序,或许你会直接(本机或远端)登入 Linux,打开编辑器写完代码后,就用 gcc/g++ 来编译,遇到要除错(debug)的时候,则会选择使用 gdb 来进行除 ...

  6. BI之SSAS完整实战教程5 -- 详解多维数据集结构

    之前简单介绍过多维数据集(Cube)的结构. 原来计划将Cube结构这部分内容打散,在实验中穿插讲解, 考虑到结构之间不同的部分都有联系,如果打散了将反而不好理解,还是直接一次性全部讲完. 本篇我们将 ...

  7. (旧)子数涵数·C语言——条件语句

    首先,我们讲一下理论知识,在编程中有三种结构,分别是顺序结构.条件结构.循环结构,如果用流程图来表示的话就是: 那么在C语言中,如何灵活运用这三种结构呢?这就需要用到控制语句了. 而条件语句便是控制语 ...

  8. domain规划

    user-generated content 用户产生内容 和 admin-generated content 管理员产生内容,要区分开来,便于不同的图片压缩.备份.带宽.CDN.审核方案. 图片(j ...

  9. 使用openssl创建自签名证书及部署到IIS教程

    概要 本文讲解三个部分:1. 创建自签名证书2. 创建自己的证书颁发机构3. 以及如何配置IIS 创建自签名证书 首先,创建一个私钥文件: openssl genrsa -out myselfsign ...

  10. pace.js和NProgress.js两个加载进度插件的一点小总结

    这两个插件都是关于加载进度动画的,应该说各有特点吧,最起码对我来说是各有优势的.今天一天就捣鼓了加载进度动画,也研究了大量的(也就这两个)加载进度动画,也算对加载进度动画有了一个初步的了解了吧. NP ...