Unicorn Stable Arrangement
Time limit5sMemory limit512 MB
Given counts of red, yellow, and blue unicorns, arrange them in a ring so no two neighbors share a hair color, or report IMPOSSIBLE, printing the lexicographically smallest valid string.
- Level
Medium5 of 10
- Topics
- Greedy, Implementation, String, Math
- Solved
- No attempts yet
Problem
You own pet unicorns. Each mane holds one or two kinds of hair among red hairs, yellow hairs, and blue hairs, and the color of a mane depends on exactly which kinds it holds.
- A mane with only one kind of hair appears to be that color. A mane with only blue hairs is blue.
- A mane with red and yellow hairs appears orange.
- A mane with yellow and blue hairs appears green.
- A mane with red and blue hairs appears violet.
You have , , , , , and unicorns with red, orange, yellow, green, blue, and violet manes.
You have just built a circular stable with stalls arranged in a ring, so every stall borders two other stalls. You want to put exactly one unicorn in each stall. Unicorns need to feel rare, so a unicorn cannot stand next to another unicorn whose mane holds at least one of the same hair colors. A unicorn with an orange mane cannot stand next to a unicorn with a violet mane, because both manes hold red hairs. A unicorn with a green mane cannot stand next to a unicorn with a yellow mane, because both manes hold yellow hairs.
Decide whether all of the unicorns can be placed, and produce an arrangement when they can.
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
- for each in
- (every unicorn has a single hair color in its mane)
Output
For each test case, print one line Case #x: y, where is the test case number starting from 1. If the unicorns cannot all be placed, is IMPOSSIBLE. Otherwise is a string of characters giving the unicorns in the stalls, starting at a stall of your choice and reading clockwise around the ring. Write R for a unicorn with a red mane, O for orange, Y for yellow, G for green, B for blue, and V for violet.
Several strings can describe a valid arrangement, including the strings you get by starting at a different stall. Collect all of them and print only the lexicographically smallest one. Characters compare in ASCII order, so B < G < O < R < V < Y.
Note
The stalls form a ring, so the first character and the last character of the printed string are neighbors as well.