#C1635. Ship Container Allocation
Ship Container Allocation
Ship Container Allocation
You are given (t) test cases. In each test case, there are (n) containers with given weights and (m) ships with specified weight capacities. Your task is to allocate as many containers as possible to the ships under the following constraint: for each ship with capacity (C), containers may be loaded sequentially provided that the cumulative weight does not exceed (C). Formally, if the current total weight loaded on a ship is (current) and the next container has weight (w_i), then the container can be loaded if and only if
[ current + w_i \le C ]
Each container may be allocated at most once. An optimal strategy is to sort the container weights in ascending order and the ship capacities in descending order, then greedily assign containers to ships. Solve the problem by computing, for each test case, the maximum number of containers that can be loaded without violating any ship's capacity.
inputFormat
The input begins with an integer (t) representing the number of test cases. For each test case:
- The first line contains two integers (n) and (m), representing the number of containers and the number of ships, respectively.
- The second line contains (n) space-separated integers denoting the weights of the containers.
- The third line contains (m) space-separated integers representing the capacities of the ships.
outputFormat
For each test case, output a single integer on a new line indicating the maximum number of containers that can be allocated to the ships without exceeding their capacities.## sample
3
4 2
2 3 7 5
10 8
5 3
8 2 4 2 7
10 12 15
3 1
3 4 5
10
4
5
2
</p>