1984 ACM East Central Regional Programming Contest

Problem #3: Permutation Inversion Tables

Source file: prob3.pas
Input file: prob3.in
Output file: prob3.out

A permutation of the first n integers { 1, 2, 3, ..., n } is a list where each of the integers appears exactly once. For example, the six permutations of the integers { 1, 2, 3 } are: 1 2 3, 1 3 2, 2 1 3, 2 3 1, 3 1 2, and 3 2 1.

There are several methods used to represent a permutation of the integers { 1, 2, 3, ..., n }, one of which is to show the permutation directly as was done above for each of the six permutations of { 1, 2, 3 }.

A second method is to use the inversion table for a permutation. If a1a2a3...an is a permutation (listing method) of { 1, 2, 3, ..., n } then an inversion of this permutation is an ordered pair (ai, aj) such that i < j and ai > aj. For example, the permutation 5 2 3 1 4 contains the six inversions (5,2), (5,3), (5,1), (5,4), (2,1), (3,1).

Corresponding to a permutation a1a2a3...an there is a sequence of integers b1b2b3...bn such that bj is the number of elements to the left of j in a1a2a3...an which are greater than j. Equivalently, bj is the number of inversions with second coordinate equal to j. The sequence b1b2b3...bn is called the inversion table for the permutation a1a2a3...an. For example, the permutation 5 2 3 1 4 yields the inversion table 3 1 1 1 0.

A permutation determines a unique inversion table; furthermore, given the inversion table it is possible to reconstruct the associated permutation.

PROBLEM: Determine whether or not a sequence of integers of length n represents an inversion table for a permutation of { 1, 2, 3, ..., n }. If it does, reconstruct the permutation, otherwise, indicate that it represents no valid permutation of the first n positive integers.

Each record consists of an integer n for 1 <= n <= 20 followed by n more integers which may or may not represent an inversion table for some permutation of { 1, 2, 3, ..., n}. The integers are right justified in successive fields of width three. The first integer in each input record represents the length of the possible inversion table which follows, and should not be considered part of the table.

For each input record there are to be two lines of output. The first line should contain the sequence of integers (not including the integer representing the length) which was read. The second line should contain one of two possibilities. If the sequence is an inversion table, the second line should contain the reconstructed permutation. If the input sequence is not an inversion table, the second line should contain a sequence of asterisks, with exactly one asterisk under each number printed on the first line. All integers and asterisks are to be printed right justified in fields of width three. The data will be terminated by a zero appearing in the first field of the last line.

Sample Input:

9  3  3  1  5  2  3  0  0  0
3  2  2  1
5  3  1  1  1  0
0

Corresponding Output:

3  3  1  5  2  3  0  0  0
7  3  8  1  2  5  9  6  4
2  2  1
*  *  *
3  1  1  1  0
5  2  3  1  4