Pearl Pairing
InterviewTime limit1sMemory limit128 MB
Given counts of each pearl color, output the canonical pairing that matches the pearl at position i with the pearl at position i + N/2 in sorted order.
- Level
Medium5 of 10
- Topics
- Greedy, Sorting, Implementation, Math
- Solved
- No attempts yet
Problem
Bessie received pearls for her birthday (, with always even). Each pearl is painted one of different colors (); the colors are numbered through , and color is used on exactly pearls, so .
Because is even, Bessie wants to arrange all of the pearls into pairs so that the two pearls in each pair have different colors. It is guaranteed that at least one such pairing exists for the given input.
Because many valid pairings may exist, you must output one specific canonical pairing, defined precisely in the output section below.
Input
- Line : two space-separated integers and .
- Lines : line contains a single integer , the number of pearls of color .
Output
List the pearls in non-decreasing order of color: first all pearls of color , then all pearls of color , and so on through color . Number the pearls in this order from to .
Form pairs by matching the pearl in position with the pearl in position , for . Because a valid pairing is guaranteed to exist, the two pearls of every such pair always have different colors.
Print lines. On line , print the two colors of the -th pair, the smaller color first, separated by a single space.
Hint
In the sample, color is used on pearls, and colors and are each used on pearls. Under the canonical construction, each of the four color- pearls is paired with a pearl of color or color .