#K64187. Organize Departments
Organize Departments
Organize Departments
You are given several test cases. In each test case, you are provided with a list of employees. Each employee has an ID and a flag indicating whether they are a manager (1 means manager, 0 means non-manager). Your task is to organize these employees into departments such that each department has at least one manager and no department contains more than 3 employees in total.
The grouping is done as follows: For each test case, first, extract all managers and non-managers in the order they appear. Then, assign each manager to a separate department and, if available, greedily add non-managers to that department until its size reaches 3. If after assigning all managers there are still non-managers left, or if a test case does not contain any manager, then the organization is deemed impossible. In such cases, output "Impossible" for that test case.
Note: The capacity of a department can be represented by the inequality \( |\text{department}| \leq 3 \) and since at least one manager is required, at most 2 non-managers can be added with a manager.
inputFormat
The input is given via stdin and has the following format:
T N ID1 is_manager1 ID2 is_manager2 ... IDN is_managerN
Where:
- T is the number of test cases.
- For each test case:
- N is the number of employees.
- Followed by N lines, each with an employee ID (a string) and a boolean flag (0 or 1) separated by space. 1 indicates a manager.
- If a valid organization is possible, print each department on a separate line. Each department is represented by employee IDs separated by a single space, in the order they were assigned.
- If organization isn’t possible for a test case, print a single line with
Impossible
.
outputFormat
Output must be printed to stdout. For each test case:
3
4
abc123 1
xyz456 0
lmn789 0
pqr012 1
6
mgr05 1
emp99 0
emp01 0
mgr22 1
emp11 0
emp33 0
3
empA 0
empB 0
empC 0
abc123 xyz456 lmn789
pqr012
mgr05 emp99 emp01
mgr22 emp11 emp33
Impossible
</p>