Stable Neighbors
Time limit5sMemory limit512 MB
Place given counts of six mane colors around a ring so that no two neighbors share a base hair color, and output the lexicographically smallest valid arrangement.
- Level
Hard8 of 10
- Topics
- Greedy, Implementation, Math, Brute force
- Solved
- No attempts yet
Problem
You keep pet unicorns. The mane of a unicorn holds one or two kinds of hair among red, yellow and blue, and the color of the mane depends on exactly which kinds it holds.
- A mane with a single kind of hair looks like that color. A mane with only blue hairs is blue.
- A mane with red hairs and yellow hairs looks orange.
- A mane with yellow hairs and blue hairs looks green.
- A mane with red hairs and blue hairs looks violet.
You have , , , , and unicorns whose manes are red, orange, yellow, green, blue and violet.
You built a stable with stalls arranged in a ring, so every stall borders exactly two other stalls. You want to put exactly one unicorn in each stall. Two unicorns cannot be neighbors if their manes share at least one kind of hair. An orange mane cannot sit next to a violet mane, because both hold red hairs, and a green mane cannot sit next to a yellow mane, because both hold yellow hairs.
Decide whether all the unicorns fit, and print an arrangement when they do.
Input
The first line holds the number of test cases . Each of the next lines holds one test case: seven integers , , , , , and separated by spaces.
Limits
- , , , , and are all at least .
Output
For each test case, print one line in the form Case #x: y, where is the test case number starting from .
If the unicorns do not all fit, is IMPOSSIBLE. Otherwise is a string of characters that lists the arrangement, read clockwise from a stall of your choice. Write R for a red mane, O for orange, Y for yellow, G for green, B for blue and V for violet. The stalls form a ring, so the first character and the last character are neighbors too.
When several arrangements work, print only the string that comes first in lexicographic order among all strings you can obtain, including every choice of the starting stall. The letters order as B, G, O, R, V, Y.
Notes
In a stable with three stalls every stall borders the other two, so two unicorns with the same mane color can never both be placed.
With two unicorns each of the red, yellow and blue manes, BYBRYR obeys the rules. It is not the answer, because another valid arrangement comes earlier in lexicographic order. Under the same counts BYRYRB breaks the rules, since its last B and its first B are neighbors.
BRYBGR breaks the rules as well. A blue mane sits next to a green mane, and both hold blue hairs.