You are on page 1of 2

8/7/2017 SPOJ.

com - Problem BABTWR

(/)
Problems (/problems) / classical (/problems/classical) / Tower of Babylon

My status (/status/BABTWR,gaurav_jain21/) Status (/status/BABTWR/) Ranking (/ranks/BABTWR/)

BABTWR - Tower of Babylon


Apart from the Hanging Gardens the Babylonians (around 3000-
539 b.c.) built the Tower of Babylon as well. The tower was meant
to reach the sky, but the project failed because of a confusion of
language imposed from much higher above.

For the 2638th anniversary a model of the tower will be rebuilt. n


di erent types of blocks are available. Each one of them may be
duplicated as many times as you like. Each type has a height y, a
width x and a depth z. The blocks are to be stacked one upon eachother so that the
resulting tower is as high as possible. Of course the blocks can be rotated as desired
before stacking. However for reasons of stability a block can only be stacked upon
another if both of its baselines are shorter.

Input
The number of types of blocks n is located in the rst line of each test case. On the
subsequent n lines the height yi, the width xi and the depth zi of each type of blocks are
given. There are never more than 30 di erent types available.

There are many test cases, which come one by one. Input terminates with n = 0.

Edited: You can assume that max(xi, yi, zi) <= 2500.

Output
For each test case your program should output one line with the height of the highest
possible tower.

Example

Sample input:
5
31 41 59
26 53 58
97 93 23
84 62 64
33 83 27
1
1 1 1
0

Sample output:
342
1

Submit solution! (/submit/BABTWR/)

hide comments

< Previous 1 2 (/problems/BABTWR/cstart=10)

Next (/problems/BABTWR/cstart=10) > (/problems/BABTWR/cstart=10)

aman_9899 (/users/aman_9899): 2017-06-21 16:21:08


box stacking problem ..!!!
nice dp

http://www.spoj.com/problems/BABTWR/ 1/2
8/7/2017 SPOJ.com - Problem BABTWR
cake_is_a_lie (/users/cake_is_a_lie): 2017-02-28 15:18:02
This can be solved in O(N log N), but it's a bit tricky. There is a pretty obvious
O(N^2) solution and even more obvious O(N^3) that should still not TLE I think.

yingduchman_ "As it's not an ad-hoc, you are unlikely to invent or discover the
solution" - what a horribly self-limiting way of thinking. I discovered the O(N^2)
solution without reading either of your suggestions and I think it's simple to come
up with.

I was also well on the way to getting the O(N log N) before reading about the N log
N LIS - i.e. I gured BST or some other query structure could be used to improve
the O(N^2) and frankly, reading about LIS didn't help that much in guring out
how.

yingduchman_ (/users/ yingduchman_): 2016-12-23 05:49:40


Learn dp solution of LIS(longest increasing subsequence) rst. Then learn "box
stacking algorithm"-the problem is of. As it's not an ad-hoc, you are unlikely to
invent or discover the solution

razor123 (/users/razor123): 2016-11-09 16:02:17


3 rotations are possible if u keep (depth > width).

ashish22_dwd (/users/ashish22_dwd): 2016-10-07 15:26:38


My 50th with this nice problem :)

hamjosh1 (/users/hamjosh1): 2016-09-25 09:26:55


easy :'D

praval_singhal (/users/praval_singhal): 2016-06-30 13:24:20


AC in one go. Top-Down is great.

Mohit Rathore (/users/markroxor): 2015-08-29 10:00:17


This problem is just awesome!

agaurav77 (/users/ashishgaurav): 2014-12-31 15:49:11


This one is a nice DP question. Those who are confused about rotations, here's a
hint : the number of rotations possible depend on the dimensions. For example,
<1,1,1> has 1 rotation, whereas <1,2,3> has 6 rotations.

Rajat (1307086) (/users/rajateuler): 2014-12-29 05:04:23


Nelson number shows up with this DP. Do not cross check case 1. It is correct. use
it to check your solution.

Leave a Comment

Publish

Notes:
1. Don't post any source code here.
2. Please be careful, leave short comments only. Don't spam here.
3. For more discussion (hints, ideas, solutions) please visit our forum (/forum).
4. Authors of the problems are allowed to delete the post and use html code here (e.g. to
provide some useful links).

Submit solution! (/submit/BABTWR/)

Micha Czuczman
Added by:
(/users/ahven)
Date: 2004-07-06
Time limit: 3s
Source limit: 50000B
Memory limit: 1536MB
Cluster: Cube (Intel G860) (/clusters/)

http://www.spoj.com/problems/BABTWR/ 2/2