#K14706. Arrange Gemstones
Arrange Gemstones
Arrange Gemstones
In this problem, you are given an integer (n) and (n) gemstones. Each gemstone is represented by two numbers: its value and its initial position. Your task is to arrange these gemstones in ascending order of their values. In case two gemstones have the same value, they should be arranged in ascending order of their positions.
Formally, given a list of gemstones where each gemstone is represented as a tuple ((a_i, p_i)), you are to sort them such that in the sorted order, for any two gemstones (g_i) and (g_j) with (i < j), either (a_i < a_j), or (a_i = a_j) and (p_i < p_j).
If any two gemstones share the same position, then the arrangement is impossible and you should output Impossible
.
inputFormat
The input is read from standard input. The first line contains an integer (n), the number of gemstones. Each of the following (n) lines contains two integers separated by a space, representing the value and the initial position of a gemstone.
outputFormat
If the gemstones can be arranged under the given rules, output YES
on the first line, followed by a second line which contains the arranged gemstone positions separated by spaces. If the arrangement is impossible because of duplicate positions, output Impossible
.## sample
5
2 5
1 3
2 1
1 2
3 4
YES
2 3 1 5 4
</p>