Rainbow Sort
시간 제한20초메모리 제한1024 MB
각 색에 서로 다른 정수를 부여해 카드의 정수가 비감소가 되도록 하고, 그 정수 순서대로 색을 출력한다. 색의 첫 등장과 마지막 등장 구간이 겹치지 않아야 하므로 구간을 위치순으로 정렬하는 문제로 바뀐다. 탐욕적으로 훑으면서 교차하는 색을 찾으면 답을 얻거나 IMPOSSIBLE을 판정한다.
문제
Your friend Charles gives you a challenge. He puts cards on a table and arranges them in a line in an order that he chooses. Each card has a single color, and each color can be on one or more cards.
Charles then asks you to write a positive integer on each card without altering his chosen order such that:
- The integers you write appear in non-decreasing order when cards are read from left to right.
- Cards of the same color have the same integer written on them.
- Cards of different colors have different integers written on them.
Finally, Charles wants you to order the colors in increasing order of written integer. For example, if blue cards have a , red cards have a , and green cards have a , the color order would be blue, green, red.
입력
The first line of the input gives the number of test cases, . test cases follow.
Each test case begins with a line containing the integer . The next line contains integers, , , , , where represents the color of the -th card from the left.
출력
For each test case, output one line containing Case #x: y, where is the test case number (starting from 1) and is the set of colors, once each, listed in the requested order. If it is impossible to write integers in the given cards while adhering to all the rules, must be IMPOSSIBLE instead.
제한
- .
- , for all .
힌트
In Sample Case #1, there are different colors on cards. One possible solution is to write the following integers, in order: , , , and . Notice that the same integer () is written on both cards of color . Then, the order of the colors is , , .
In Sample Case #2, let and be the integers written in cards of color and , respectively. If then the rightmost two cards would not have their integers in non-decreasing order. If that would happen to the second and third card from the left. Finally, is forbidden by one of the rules. Therefore, there is no valid way of writing the integers in this case.