**Input**

**Output**

**My question:**

Started by noatom, Apr 28 2013 05:33 AM

3 replies to this topic

Posted 28 April 2013 - 05:33 AM

You have a large rectangle paper sheet of size W x H. You need to cut Wix Hisized pieces from this

rectangular sheet. The problem is that the paper sheet has a special coating and it can only be cut

with a special method.

To cut the paper, first you have to fold it in two pieces, and then cut through the whole length of the

fold to produce 2 smaller rectangular pieces. Thus, each cut must span the whole length of the

rectangular piece. You will repeat this fold & cut operation on the smaller pieces until you produce

all the required rectangular pieces.

The paper's coating also has a special pattern on it which prevents rotating of the cut pieces. This

prevents cutting a Hix Wipiece instead of a Wix Hipiece.

The input file will contain the dimensions of the paper (W, H), number of piece types (N), and the

size of the piece types (Wi, Hi).

First line will contain W and H.

Second line will contain N.

The following N lines will contain Wi and Hi.

The output file will only contain N+1 lines. The first line will contain a single integer number – the

minimum amount of paper wasted. The next N lines will contain the number of pieces that will be

cut, for each piece type.

Example:

Input:

42 22

4

12 4

14 10

20 8

30 20

output:

40

3

3

2

0

The problem is the example.

If I have a rectangle of size 42 x 22 as above.And I want to cut it in pieces of 30 x 20.In the above example,it shows me that the pieces I will get will be 0.How is that possible? Shouldn't it be 1?!

Posted 28 April 2013 - 07:06 AM

My guess would be that the aim is to minimize the paper wastage, and to do that in the example case it's not helpful to use the 30x20 sized piece (because that would leave a 22x2=44 sized piece left over, after filling the remaining space with 12x4 pieces).

That is:

(12x4x3) + (14x10x3) + (20x8x2) = 884

(42x22)-884 = 40 (the total paper wastage)

Posted 28 April 2013 - 05:34 PM

I wrote a crude dynamic programming -based brute force solution since I haven't had the chance to use DP before. It can deal with this input, which results in 2.1M pieces, in 10 seconds on an old laptop:

10000 10000

4

12 4

14 10

20 8

30 20

but jumping up to a 15000^2 problem (result 4.7M pieces) takes minutes, I presume because physical memory runs out. I'm kind of itching to try packing the memory. Instead of piece counts and minimum cost, I could memoize only the cost and the index of the "top-level" piece of an optimal configuration for huge memory savings. It shouldn't be hugely expensive to recurse through the configurations at the end to do a final count.

10000 10000

4

12 4

14 10

20 8

30 20

but jumping up to a 15000^2 problem (result 4.7M pieces) takes minutes, I presume because physical memory runs out. I'm kind of itching to try packing the memory. Instead of piece counts and minimum cost, I could memoize only the cost and the index of the "top-level" piece of an optimal configuration for huge memory savings. It shouldn't be hugely expensive to recurse through the configurations at the end to do a final count.

Posted 28 April 2013 - 06:18 PM

If I have a rectangle of size 42 x 22 as above.And I want to cut it in pieces of 30 x 20.In the above example,it shows me that the pieces I will get will be 0.How is that possible? Shouldn't it be 1?!

To expand on what Adam_42 said, it sounds like the textbook is asking you to come up with a certain number of cuts of each size such that the minimum amount of paper is wasted.

To give an analogous problem: I have a $3.50. There are 4 things in the store I can buy, all of them costing different prices. Let's say item 1 is $0.50, item 2 is $1, item 3 is $2, and item 4 is $3.25. What should I buy such that I have the least amount of money left? Sure, I could afford to buy one of item #4. But that would leave me with $0.25 remaining, which isn't the "least amount of money left" (because if I instead bought one of items 1, 2, and 3, I would spend $3.50 total and have 0 left, which is more "optimal").

Similarly, in your problem, it's not about "what can you cut?" It's about "what should you cut to get the least waste?"

[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]