Waterloo 2025 Fall C - Magic Matrix
View as PDFGenerative AI models can do many impressive things. They almost seem like magic, even though underneath, they are just large collections of weights—just numbers. Some AI models can even solve programming contest problems, but please don't use them in a contest because that would be cheating.
Your task here is to construct a matrix with some interesting magic properties. You will be given two sequences of numbers of the same length, for example
4 6 5 and 7 8 9. You then need to construct a matrix of integers from 1 to 3 (inclusive) of size by
(so 15 by 24), such that:
- In exactly 4 of the rows, strictly more than half the integers are 1
- In exactly 6 of the rows, strictly more than half of the integers are 2
- In exactly 5 of the rows, strictly more than half of the integers are 3
- In exactly 7 of the columns, strictly more than half the integers are 1
- In exactly 8 of the columns, strictly more than half the integers are 2
- In exactly 9 of the columns, strictly more than half the integers are 3
Hopefully, by now you already see how such a matrix can be constructed, as the task at hand is to do so in the general case.
Input Specification
The first line of input contains an integer , with
, the length of the two given sequences.
The second line contains integers
, with
, specifying that in exactly
rows of the matrix, strictly more than half of the integers should be
. The sum of all the
is at most
.
The third line contains integers
with
, specifying that in exactly
columns of the matrix, strictly more than half of the integers should be
. The sum of all the
is at most
.
Output Specification
If the matrix cannot be formed, output a single line containing the word NO. If the matrix can be formed, output a line containing the word YES, followed by the rows of the matrix, one row per line, with a space separating the integers within a row. If there are multiple possible matrices, output any one of them.
Sample Input 1
2
2 4
3 2
Sample Output 1
YES
2 2 1 1 2
2 1 1 1 1
1 1 2 1 1
2 1 2 1 2
2 2 1 2 2
2 1 1 2 2
Sample Input 2
3
2 2 2
2 2 2
Sample Output 2
YES
3 2 2 1 2 2
3 1 2 1 1 1
2 1 2 2 3 2
1 1 1 1 3 2
3 3 2 1 3 3
3 1 3 3 3 2
Comments
🎉🎉🎉