#P6411. Lexicographically Smallest Symmetric Character Matrix
Lexicographically Smallest Symmetric Character Matrix
Lexicographically Smallest Symmetric Character Matrix
You are given a positive integer , an integer , and different characters with their counts. Your task is to construct an character matrix satisfying the following conditions:
- The matrix is symmetric, i.e. for all .
- The matrix contains exactly copies of character for each (), and ( \sum_{i=1}^{k} a_i = n^2 ).
Among all possible matrices, you must output the one that is lexicographically smallest (i.e. if the matrix is read row‐by‐row, the resulting string is minimum in lexicographical order).
Note that printing the full matrix might be too much. Hence, you are only required to output the first columns of the matrix.
If no valid matrix exists, print IMPOSSIBLE
.
Construction idea and mathematical formulation
Because the matrix is symmetric, the diagonal cells appear only once while every off-diagonal cell is paired with its symmetric counterpart. In a valid solution, if a character appears in the diagonal times, then the remaining occurrences must be even (since they are placed in pairs). Further, if is odd then must be odd, and if is even then must be even.
Let the number of diagonal cells be . First, assign the mandatory diagonal counts: for each character , if is odd then assign one occurrence in the diagonal. Let be the total number of such mandatory assignments. The remaining diagonal positions, say , have to be filled with letters in pairs (i.e. 2 at a time from the same character) so that the leftover counts (assigned to off-diagonals) remain even.
The lexicographically smallest solution is obtained by choosing the smallest possible letter for each position (first on diagonals in increasing order of indices, and then for each off-diagonal pair in lexicographical order).
Input/Output Format
The input format is as follows:
n p k c1 a1 c2 a2 ... ck ak
Here, and are positive integers, and is the number of distinct characters. For each of the next lines, a character and its count are provided. It is guaranteed that ( \sum_{i=1}^{k} a_i = n^2 ).
The output format is as follows:
- If a valid matrix exists, output lines; each line is a string of length representing the first columns of that row of the matrix.
- Otherwise, output a single line with
IMPOSSIBLE
.
inputFormat
The first line contains three integers: n, p, and k, where n is the size of the matrix, p is the number of columns to output (p ≤ n), and k is the number of distinct characters. Then follow k lines. Each of these lines contains a character and an integer a, meaning that character appears exactly a times in the matrix. It is guaranteed that \( \sum_{i=1}^k a = n^2 \).
outputFormat
If a valid symmetric matrix satisfying the given constraints exists, output n lines where each line is the concatenation of the first p characters of that row. Otherwise, output a single line with the string "IMPOSSIBLE".
sample
3 2 2
A 5
B 4
AA
AA
BB
</p>