#K44467. Passenger Grouping Under Capacity Constraint
Passenger Grouping Under Capacity Constraint
Passenger Grouping Under Capacity Constraint
Given a vehicle with capacity \(C\) and a list of passenger weights, your task is to group the passengers such that the total weight in each group does not exceed \(C\). The grouping must follow a greedy strategy: first sort the weights in descending order, and then repeatedly form a group by selecting the heaviest available passenger and then adding any other passengers that can fit without exceeding the capacity.
If it is impossible to place a passenger into any group (i.e. a passenger's weight is greater than \(C\)) or if no passenger is given, then the output should be a single line with []
.
inputFormat
The input is read from standard input (stdin) and consists of two lines:
- The first line contains a single integer \(C\) (\(1 \le C \le 10^9\)), representing the vehicle's capacity.
- The second line contains space-separated integers denoting the weights of the passengers. If there are no passengers, the second line will be empty.
outputFormat
If a valid grouping is found, print each group on a separate line. Within each line, output the weights (space‐separated) that form that group. The groups can be printed in any order. If it is impossible to form a valid grouping (or if the input list is empty), output a single line containing []
.
100
20 30 50 70 90 10
90 10
70 30
50 20
</p>