Box Fitting (Codeforces 1498B)
Tags: Greedy, bitmasks

You are given $$$n$$$ rectangles, each of height $$$1$$$. Each rectangle's width is a power of $$$2$$$ (i. e. it can be represented as $$$2^x$$$ for some non-negative integer $$$x$$$). You are also given a two-dimensional box of width $$$W$$$. Note that $$$W$$$ may or may not be a power of $$$2$$$. Moreover, $$$W$$$ 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.

Input: The first line of input contains one integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^3$$$) — the number of test cases. Each test case consists of two lines.For each test case: the first line contains two integers $$$n$$$ ($$$1 \le n \le 10^5$$$) and $$$W$$$ ($$$1 \le W \le 10^9$$$); the second line contains $$$n$$$ integers $$$w_1, w_2, \dots, w_n$$$ ($$$1 \le w_i \le 10^6$$$), where $$$w_i$$$ is the width of the $$$i$$$-th rectangle. Each $$$w_i$$$ is a power of $$$2$$$; additionally, $$$\max\limits_{i=1}^{n} w_i \le W$$$. The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

Output: Output $$$t$$$ integers. The $$$i$$$-th integer should be equal to the answer to the $$$i$$$-th test case — the smallest height of the box.

Note: 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:  In the figure above, the number inside each rectangle is its width. The width of the 2D box is $$$16$$$ (indicated with arrow below). The minimum height required for the 2D box in this case is $$$2$$$ (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.

Sample Inputs:
2
5 16
1 2 8 4 8
6 10
2 8 8 2 2 8

Sample Outputs:
2
3

Solution (Python):
from collections import *
from itertools import *
from bisect import *
from heapq import *
from math import ceil, prod, gcd, factorial
from time import time
mod = 10 ** 9 + 7
d = ((1, 0), (0, 1), (0, -1), (-1, 0))
alpha = 'abcdefghijklmnopqrstuvwxyz'

def genPrimes(n):
    n += 1
    arr = [0, 1] * (n // 2)
    arr[1] = 0
    arr[2] = 1
    if n % 2:
        arr += [0]
    i = 3
    while i * i < n:
        if arr[i]:
            arr[i * i:n:i] = [0] * (1 + (n - i * i - 1) // i)
        i += 2
    return [prime for (prime, flag) in enumerate(arr) if flag]

def helper(target):
    return max((abs(target - arr[i]) + brr[i] for i in range(n)))
for _ in range(int(input())):
    (n, w) = map(int, input().split())
    arr = list(map(int, input().split()))
    store = defaultdict(int)
    for item in arr:
        store[item] += 1
    keys = sorted(store.keys(), reverse=True)
    val = 0
    count = -1
    while val < w:
        count += 1
        val = w
        for key in keys:
            while store[key] and val >= key:
                store[key] -= 1
                val -= key
    print(count)

Solution (Python):
import bisect
t = int(input())
while t > 0:
    (n, w) = map(int, input().split())
    l = list(map(int, input().split()))
    l.sort()
    l.reverse()
    ans = []
    c = 0
    for j in l:
        i = bisect.bisect_left(ans, j)
        if i < len(ans):
            ans[i] = ans[i] - j
        else:
            ans.append(w - j)
            c += 1
    print(c)
    t = t - 1

Solution (Python):
for _ in range(int(input())):
    (n, width) = map(int, input().split())
    arr = list(map(int, input().split()))
    arr.sort(reverse=True)
    dict_ = {}
    for i in arr:
        if not i in dict_.keys():
            dict_[i] = 0
        dict_[i] += 1
    ans = 0
    while n:
        aux = width
        for i in dict_:
            while i <= aux and dict_[i]:
                n -= 1
                aux -= i
                dict_[i] -= 1
        ans += 1
    print(ans)