首页 > 其他分享 >UVa 103 Stacking Boxes (DP&DAG)

UVa 103 Stacking Boxes (DP&DAG)

时间:2023-04-12 13:06:19浏览次数:44  
标签:box DAG string Stacking boxes int Boxes nesting dimensions


103 - Stacking Boxes

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=39

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 23 78 105 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


53 1 2 4 547 2 5 6



学英语:nest有“嵌套”之意。

DAG模型的一道好题。(参考《入门经典》)


完整代码:

/*0.012s*/

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 50;

int n, w, dp[N], vis[N], seq[N][N];

inline bool judge(int a, int b)
{
	for (int i = 0; i < w; i++)
		if (seq[a][i] <= seq[b][i])
			return false;
	return true;
}

int find(int d)
{
	if (dp[d])	
        return dp[d];
	for (int i = 0; i < n; i++)
		if (judge(i, d))///i可以嵌套d
		{
			int a = find(i);
			if (a > dp[d])
			{
				dp[d] = a;
				vis[d] = i;///标记,从d指向i(小的指向大的)
			}
		}
	return ++dp[d];
}

int main(void)
{
	while (~scanf("%d%d", &n, &w))
	{
		///init
		memset(dp, 0, sizeof(dp));
		memset(vis, 0, sizeof(vis));
		memset(seq, 0, sizeof(seq));
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < w; j++)
				scanf("%d", &seq[i][j]);
			sort(seq[i], seq[i] + w);
		}
		/
		int Max = 0, id, a;
		for (int i = 0; i < n; i++)
		{
			a = find(i);
			if (a > Max)
			{
				Max = a;
				id = i;///这里的id是最小的箱子,因为从它开始嵌套的箱子数最大
			}
		}
		printf("%d\n", Max);
		for (int i = 1; i < Max; i++)
		{
			printf("%d ", id + 1);
			id = vis[id];
		}
		printf("%d\n", id + 1);
	}
	return 0;
}




标签:box,DAG,string,Stacking,boxes,int,Boxes,nesting,dimensions
From: https://blog.51cto.com/u_5535544/6185478

相关文章

  • POJ - 3616 Milking Time(DAG)
    题目大意:给出N头牛的产奶时间段和产奶量,每接完一头牛的奶后,需要休息R分钟问如何选择牛,才能使接到的产奶量达到最大解题思路:DAG,按产奶的结束时间大小排序#include<cstdio>#include<cstring>#include<algorithm>usingnamespacestd;constintN=1010;structInterval{......
  • Pencils and Boxes CF985E
    给出nn个整数a1,a2,...,an,现在需要对其进行分组,使其满足以下条件:每个数都必须恰好分入一组中每一组中必须至少包含K个数在每一组中,整数的权值之差的绝对值<=D。......
  • 如何在 GNOME Boxes中的宿主机和客体机之间共享一个文件夹
    导读使用下面的步骤在GNOMEBoxes应用中的宿主机和客体机之间共享一个文件夹。GNOMEBoxes是一个创建和管理虚拟机的前端应用。它主要是为GNOME桌面开发的。......
  • Java实现一个轻量的DAG任务调度demo
    DAG(DirectedAcyclicGraph,有向无环图)是指一个有向图,其中不包含任何环。在任务调度中,DAG被用来表示任务之间的依赖关系。一个任务的执行必须等待其依赖的任务完成之后才能......
  • exchange2016服务器的DAG的部署
    现在大家都开始用exchange2016的邮件系统了。原先的的exchange2010也需要升级了。要么升级,要exchange2010和exchange2016共存。我是在把原来虚拟机的环境卸载后重新安装的......
  • 带标号弱连通DAG计数
    带标号弱连通DAG计数前言:前段时间做到了一个无向图边定向的题,就一直没搞懂其中的容斥,今天终于弄懂了。题意:对弱连通带标号的简单DAG计数,\(n\leq10^5\)。“弱连通”......
  • DAG的深度优先搜索标记
    对于在图G上进行深度优先搜索算法所产生的深度优先森林Gt,我们可以定义四种边的类型:1.树边(TreeEdge):为深度优先森林中Gt的边。如果结点v是因算法对边(u,v)的搜索而首先被发......
  • DAG的深度优先搜索标记
    一、知识对于在图G上进行深度优先搜索算法所产生的深度优先森林Gt,我们可以定义四种边的类型:1.树边(TreeEdge):为深度优先森林中Gt的边。如果结点v是因算法对边(u,v)的搜索而......
  • CodeForces - 1066D Boxes Packing 没做出来 题意 思维
    D.BoxesPackingtimelimitpertest1secondmemorylimitpertest256megabytesinputstandardinputoutputstandardoutputMaksimhas nn objectsand mm boxes,eac......
  • 缩点-DAG 性质的应用:P2002,P1262,P2341,P1407,P2746(P2812)
    缩点不只用于转化图为DAG,还可以进一步发掘图的性质,从而将题目变成结论题所求信息转化到图上,方便建模。P2002https://www.luogu.com.cn/problem/P2002在一个强连通分量......