Ultraviolet A 103-stacking boxes

Source: Internet
Author: User

[Question link]

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

[Original question]

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 ofN-Dimen1_hypercube. The former is much more complicated than its one dimen1_relative while the latter bears a remarkable resesponance to its ''lower-class'' cousin.

The Problem

ConsiderN-Dimenstm''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 (,) 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 groupN-Dimen1_boxes. You are to determine the longestNesting stringOf 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 = () nests in the box E = () since D can be rearranged) 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 = (,) since no rearrangement of D results in a box that satisfies the nesting property, but f =) 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 = ()NestsIn 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 number of boxesKIn the sequence followed by the dimensionality of the boxes,N(ON
The same line .)

This line is followedKLines, one line per box withNMeasurements 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 shocould process all of them and determine, for each sequence, which ofKBoxes determine the longest nesting string and
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 ''nermost'' box of the nesting string shocould be listed first, the next box (if there is one) shocould be listed second, etc.

The boxes shoshould 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 29 1121 188 65 2 20 1 30 1023 15 7 9 11 340 50 34 24 14 49 10 11 12 13 1431 4 18 8 27 1744 32 13 19 41 191 2 3 4 5 680 37 47 18 21 9

Sample output

53 1 2 4 547 2 5 6


[Topic]

For n-dimensional images, their edge lengths are {D1, D2, D3... DN}. For two n-dimensional images, if all the side lengths of one of them are in any order and correspond to the side lengths smaller than the other, the locks can be nested into the other. For example, a {1, 2}, B {2, 3}, and a all sides have one to one corresponding length smaller than B, so a can be nested in B.

For k N-dimensional images, find the maximum number of consecutive nesting records.


[Analysis and Summary]

First, you need to determine whether two images can be nested. You only need to sort the edges of all images in ascending order. For A and B, you only need to compare their edges one by one in order, if all AI <Bi is satisfied, A can be nested in B.

There are two solutions for this question.


The first type is the so-called use of memory-based search to find the Dag model (for details, see p161 in algorithm entry classic ).

We can use the graph's Adjacent matrix to represent all the relationships. A can be nested in B, so g [a] [B] = 1. Then, this problem can be converted to finding the maximum path length of the image.


The second method assumes that a can be nested in B and expressed by a <B, the longest string is a <B <C <D <e ..., it can be seen that it is like a "Longest incrementing subsequence". Here "incrementing" refers to "dimension" incrementing.

[Code]

Method 1: memory-based Dag search model

/** Ultraviolet A: 103-stacking boxes * memory-based search * Time: 0.016 S * Author: d_double **/# include <iostream> # include <cstring> # include <cstdio> # include <algorithm> using namespace STD; int G [32] [32], arr [32] [12], d [32], n, k; bool first; // determine whether arr [a] is larger than arr [B]. inline bool isbigger (int A, int B) {for (INT I = 0; I <K; ++ I) if (ARR [a] [I] <= arr [B] [I]) return false; return true;} int dp (int I) {If (d [I]! =-1) return d [I]; Int & Ans = d [I] = 1; for (Int J = 0; j <n; ++ J) if (G [I] [J]) {ans = max (ANS, dp (j) + 1);} return ans;} void print (int I) {If (first) printf ("% d", I + 1); else {printf ("% d", I + 1); first = true ;} // printf ("% d", I + 1); For (Int J = 0; j <n; ++ J) if (G [I] [J] & D [J] + 1 = d [I]) {print (j); break ;}} int main () {While (~ Scanf ("% d", & N, & K) {for (INT I = 0; I <n; ++ I) {for (Int J = 0; j <K; ++ J) scanf ("% d", & arr [I] [J]); sort (ARR [I], arr [I] + k);} memset (G, 0, sizeof (g); For (INT I = 0; I <n-1; ++ I) {for (Int J = I + 1; j <n; ++ J) {If (isbigger (J, I) g [I] [J] = 1; else if (isbigger (I, j) g [J] [I] = 1 ;}} memset (D,-1, sizeof (d )); int ans =-1, Pos; For (INT I = 0; I <n; ++ I) {int T = dp (I); If (T> ans) {ans = T; Pos = I ;}} printf ("% d \ n", ANS); First = false; print (POS ); printf ("\ n");} return 0 ;}

Method 2: longest incrementing subsequence

/** Ultraviolet A: 103-stacking boxes * longest incrementing sub-sequence * Time: 0.056 S * Author: d_double **/# include <iostream> # include <cstring> # include <cstdio> # include <algorithm> using namespace STD; int G [32] [32], d [32], s [32], n, k; bool first; struct node {int no; int A [12]; int K; void sort () {sort (A, A + k);} friend bool operator <(const node & A, const node & B) {for (INT I = 0; I <. k; ++ I) {if (. A [I]> B. A [I]) return false;} return tr UE ;}} arr [32]; // determines whether arr [a] is larger than arr [B]. inline bool isbigger (int A, int B) {for (INT I = 0; I <K; ++ I) if (ARR [A]. A [I] <= arr [B]. A [I]) return false; return true;} void print (int I) {If (s [I]! = I) print (s [I]); printf ("% d", arr [I]. No);} int main () {While (~ Scanf ("% d", & N, & K) {for (INT I = 0; I <n; ++ I) {for (Int J = 0; j <K; ++ J) scanf ("% d", & arr [I]. A [J]); arr [I]. no = I + 1; arr [I]. k = K; arr [I]. sort ();} Sort (ARR, arr + n); memset (G, 0, sizeof (g); For (INT I = 0; I <n-1; ++ I) {for (Int J = I + 1; j <n; ++ J) {If (isbigger (J, I )) G [I] [J] = 1 ;}int maxval = 1, Pos = 0; s [0] = 0; d [0] = 1; for (INT I = 1; I <n; ++ I) {d [I] = 1; s [I] = I; for (Int J = 0; j <I; ++ J) if (G [J] [I] & D [I] <D [J] + 1) {s [I] = J; d [I] = d [J] + 1;} If (d [I]> maxval) {pos = I; maxval = d [I] ;}} printf ("% d \ n", maxval); print (POS); printf ("\ n");} return 0 ;}

-- The meaning of life is to give it meaning.

Original Http://blog.csdn.net/shuangde800 , By d_double (reprinted, please mark)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.