Couple Destroyer Minuk (Large)
Time limit1sMemory limit1024 MB
Split a line of people into the fewest contiguous groups that can be reordered so no couple ends up adjacent, then list up to 100 valid arrangements.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Backtracking, Greedy
- Solved
- No attempts yet
Problem
Minuk, who is single, dislikes seeing couples together. He finds a waiting line of people standing in a row. He plans to split this line into groups and reorder the groups so that no couple is adjacent. The groups do not need to have the same size. However, the people inside a group cannot be reordered or reversed.
Minuk wants to be as small as possible, because a large makes reordering the groups a headache. Let be the number of valid ways when is minimal. He wants to find of these ways, chosen arbitrarily. Two ways count as different if the group order differs, even when all groups have the same size. Two ways with the same group order also count as different if any group has a different size.
Input
The first line contains an integer (), the number of people.
The second line contains integers (), separated by spaces. describes the person at position from the front of the line. If , that person is single. If and , persons and are a couple. Every nonzero value appears exactly twice.
Output
On the first line, print the minimum value of .
From the second line to line , print valid ways in total.
On line , print the group sizes for the -th way in the order group 1, group 2, group 3, , group , separated by spaces.
On line , print the group numbers from the front of the line after the groups are reordered in the -th way.
If , print on line and print nothing after that.
If no way to split the line into groups and arrange them avoids having at least one couple adjacent, print on the first line and print nothing after that.