#P7032. Generalized Circle Arrangement
Generalized Circle Arrangement
Generalized Circle Arrangement
Bob found an interesting task based on a problem from an old children’s math book. In the original task, there are 10 children standing in a circle, 5 of whom stand next to a boy and 7 stand next to a girl. For example, when 4 boys and 6 girls stand in the order
(B,G,B,G,B,G,B,G,G,G),
we have exactly 5 children adjacent to a boy and 7 adjacent to a girl. In the generalized version, you are given three integers (n, x, y) and you must determine an arrangement of (n) children (each denoted by either a boy (B) or a girl (G)) positioned in a circle such that exactly (x) children have at least one boy as a neighbor and exactly (y) children have at least one girl as a neighbor. If no valid arrangement exists, output Impossible
.
Note: The circle means that the first and last children are also neighbors. For each child, consider its two neighbors (one on the left and one on the right). A child is counted in the (x) value if at least one of his two neighbors is a boy, and similarly, it is counted in the (y) value if at least one neighbor is a girl.
Your task is to output the lexicographically smallest valid configuration (when reading from index 0 to (n-1) with (B < G)) if one exists, or output Impossible
otherwise.
inputFormat
The input consists of a single line containing three positive integers \(n, x, y\) separated by spaces:
- \(n\): the number of children in the circle.
- \(x\): the exact number of children having at least one boy in their two neighbors.
- \(y\): the exact number of children having at least one girl in their two neighbors.
outputFormat
If a valid arrangement exists, output a string of length \(n\) consisting only of the characters B
and G
representing boys and girls respectively. This string must be the lexicographically smallest among all valid configurations (with the convention that B
is considered smaller than G
). If no valid arrangement exists, output Impossible
.
sample
10 5 7
BGBGBGBGGG