#K2326. Balanced User Grouping
Balanced User Grouping
Balanced User Grouping
You are given n users and an integer m which indicates the maximum number of users allowed in any group. Each user is described by a unique id and a set of skills. The goal is to partition the users into groups such that the distribution of skills is as balanced as possible.
The grouping algorithm works as follows:
- Sort the users in descending order based on the number of skills they have. In the event of a tie, sort by ascending user id.
- Let the number of groups be determined by \(G = \left\lceil \frac{n}{m} \right\rceil\), which can be computed as \(G = \frac{n+m-1}{m}\) using integer arithmetic.
- Assign the users in a round-robin fashion to the groups: the \(i\)-th user in the sorted order will be added to group number \(i \bmod G\).
Each group will then be printed on a separate line with the user ids separated by spaces.
Note: The input is taken from stdin and the output is printed to stdout. The input follows a specific format as described below.
inputFormat
The first line contains two space-separated integers n and m where n is the number of users and m is the maximum number of users allowed in a group.
Then, there are n lines describing each user. Each of these lines begins with an integer id, followed by an integer k which indicates the number of skills for that user, and then k space-separated integers representing the user’s skills.
It is guaranteed that the id of each user is unique.
outputFormat
Output the groups, one per line. Each line should list the user ids assigned to that group in the order they were added, separated by a single space.
The number of lines in the output should be \(G\), where \(G = \left\lceil \frac{n}{m} \right\rceil\).
## sample6 3
1 3 1 2 3
2 3 2 3 4
3 3 1 4 5
4 2 2 5
5 2 1 3
6 2 3 4
1 3 5
2 4 6
</p>