아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Rainbow Sort

시간 제한20초메모리 제한1024 MB

요약
각 색에 서로 다른 정수를 부여해 카드의 정수가 비감소가 되도록 하고, 그 정수 순서대로 색을 출력한다. 색의 첫 등장과 마지막 등장 구간이 겹치지 않아야 하므로 구간을 위치순으로 정렬하는 문제로 바뀐다. 탐욕적으로 훑으면서 교차하는 색을 찾으면 답을 얻거나 IMPOSSIBLE을 판정한다.
난이도

보통10점 중 6점

유형
정렬, 그리디, 구간, 배열
정답자
아직 제출이 없습니다

문제

Your friend Charles gives you a challenge. He puts N\mathbf{N} 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:

  1. The integers you write appear in non-decreasing order when cards are read from left to right.
  2. Cards of the same color have the same integer written on them.
  3. 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 22, red cards have a 55, and green cards have a 33, the color order would be blue, green, red.

입력

The first line of the input gives the number of test cases, T\mathbf{T}. T\mathbf{T} test cases follow.

Each test case begins with a line containing the integer N\mathbf{N}. The next line contains N\mathbf{N} integers, S_1\mathbf{S\_1}, S_2\mathbf{S\_2}, …\dots, S_N\mathbf{S\_N}, where S_i\mathbf{S\_i} represents the color of the ii-th card from the left.

출력

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy 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, yy must be IMPOSSIBLE instead.

제한

  • 1≤T≤1001 \le \mathbf{T} \le 100.
  • 1≤S_i≤1051 \le \mathbf{S\_i} \le 10^5, for all ii.

힌트

In Sample Case #1, there are 33 different colors on 44 cards. One possible solution is to write the following integers, in order: 11, 22, 22, and 33. Notice that the same integer (22) is written on both cards of color 88. Then, the order of the colors is 33, 88, 22.

In Sample Case #2, let c_8c\_8 and c_2c\_2 be the integers written in cards of color 88 and 22, respectively. If c_2>c_8c\_2 \gt c\_8 then the rightmost two cards would not have their integers in non-decreasing order. If c_2<c_8c\_2 \lt c\_8 that would happen to the second and third card from the left. Finally, c_8=c_2c\_8 = c\_2 is forbidden by one of the rules. Therefore, there is no valid way of writing the integers in this case.

예제1

  1. 예제 1

    입력
    2
    4
    3 8 8 2
    5
    3 8 2 2 8
    
    예상 출력
    Case #1: 3 8 2
    Case #2: IMPOSSIBLE