[Solution] Box Fitting Codeforces Round #711

Box Fitting Solution Codeforces

You are given nn rectangles, each of height 11. Each rectangle’s width is a power of 22 (i. e. it can be represented as 2x2x for some non-negative integer xx).

You are also given a two-dimensional box of width WW. Note that WW may or may not be a power of 22. Moreover, WW is at least as large as the width of the largest rectangle.

You have to find the smallest height of this box, such that it is able to fit all the given rectangles. It is allowed to have some empty space left in this box after fitting all the rectangles.

You cannot rotate the given rectangles to make them fit into the box. Moreover, any two distinct rectangles must not overlap, i. e., any two distinct rectangles must have zero intersection area.

See notes for visual explanation of sample input.


The first line of input contains one integer tt (1≤t≤5⋅1031≤t≤5⋅103) — the number of test cases. Each test case consists of two lines.

For each test case:

  • the first line contains two integers nn (1≤n≤1051≤n≤105) and WW (1≤W≤1091≤W≤109);
  • the second line contains nn integers w1,w2,…,wnw1,w2,…,wn (1≤wi≤1061≤wi≤106), where wiwi is the width of the ii-th rectangle. Each wiwi is a power of 22;
  • additionally, maxi=1nwi≤Wmaxi=1nwi≤W.

The sum of nn over all test cases does not exceed 105105.


Output tt integers. The ii-th integer should be equal to the answer to the ii-th test case — the smallest height of the box.Example


5 16
1 2 8 4 8
6 10
2 8 8 2 2 8




For the first test case in the sample input, the following figure shows one way to fit the given five rectangles into the 2D box with minimum height:[Solution] Box Fitting Codeforces Round #711

In the figure above, the number inside each rectangle is its width. The width of the 2D box is 1616 (indicated with arrow below). The minimum height required for the 2D box in this case is 22 (indicated on the left).

In the second test case, you can have a minimum height of three by keeping two blocks (one each of widths eight and two) on each of the three levels.

Solution: Coming Soon

Also Read

Box Fitting Solution Codeforces

2 thoughts on “[Solution] Box Fitting Codeforces Round #711”

Leave a Comment