DWITE, November 2012, Problem 4
Your friend is trying to convince you of his magical powers. He decides to perform the following trick to persuade you: Given a list of shuffled cards, where each card has a unique number from written on it, he'll guess the numbers on the cards. Knowing that this is highly improbable, you call his bluff. He then continues to tell you that he still needs one more piece of information for this trick to work. For every card in the deck, he needs to know how many cards after this card has a bigger number written on it. Being a Computer Science student, you claim that you can write a program to do this simple trick as well.
The input will contain 5 test cases. Each test case consists of 2 lines. The first line will contain an integer , the number of cards in a deck. The following line will contain numbers, separated by a space. Where each number represents the count of cards in the deck, with a larger value than the card in the position of that number. e.g. a list that starts with "" says that there is card greater than the first in the deck, cards greater than the second card in the deck, etc.
The output will contain 5 lines of output. Each a line of space separated numbers representing the original order of the deck in a corresponding test case. If no such deck can be constructed, print
3 1 1 0 4 2 2 2 2
2 1 3 IMPOSSIBLE
Problem Resource: DWITE