Official

E - Max Matrix 2 Editorial by en_translator


If \(X\) contains duplicate elements, the answer is No; same goes for \(Y\). We assume this is not the case.

Consider filling the elements of \(A\) from \((N\times M)\) through \(1\) in descending order.

Suppose that we have already placed the elements greater than \(v\). The constraints for placing \(v\) are as follows:

  • If \(v\) is contained in both \(X\) and \(Y\): For \(X_i=Y_j=v\), let \(A_{i,j}\) be \(v\).
  • If \(v\) is contained only in \(X\): For \(X_i=v\), take \(j\) such that \(Y_j>v\) and \(A_{i,j}\) is undermined, and let \(A_{i,j}\) be \(v\).
  • If \(v\) is contained only in \(Y\): For \(Y_j=v\), take \(j\) such that \(X_i>v\) and \(A_{i,j}\) is undermined, and let \(A_{i,j}\) be \(v\).
  • If \(v\) is contained in neither \(X\) nor \(Y\): take \(i\) and \(j\) such that \(X_i>v\), \(Y_j>v\), and \(A_{i,j}\) is undermined, and let \(A_{i,j}\) be \(v\).

Since we are filing \(v\) in descending order, the number of candidate integers that can be filled in increases monotonically. Thus, as long as the rules above are satisfied, we may fill them freely.

All that left is to implement this idea into an implementation, but naively checking the rules above costs a large computational complexity, leading to TLE (Time Limit Exceeded). On implementation, we may regard that \(X_{i,j}\) can be freely filled once \(v < \min(X_i,Y_j)\) is satisfied, and manage \((i,j)\) with \(v=\min(X_i,Y_j)\) in lists. For more details, please refer to the sample code below.

Sample code (Python3)

import sys
from collections import deque

input = sys.stdin.readline

def solve():
    n, m = map(int, input().split())
    nm = n * m
    x = [v - 1 for v in map(int, input().split())]
    y = [v - 1 for v in map(int, input().split())]

    gx = [-1] * nm
    gy = [-1] * nm
    for i in range(n):
        if gx[x[i]] != -1:
            print("No")
            return
        gx[x[i]] = i
    for j in range(m):
        if gy[y[j]] != -1:
            print("No")
            return
        gy[y[j]] = j

    ok = [[] for _ in range(nm)]
    for i in range(n):
        for j in range(m):
            ok[min(x[i], y[j])].append((i, j))

    ans = [[-1] * m for _ in range(n)]
    q = deque()

    for v in range(nm - 1, -1, -1):
        if gx[v] == -1 and gy[v] == -1:
            if not q:
                print("No")
                return
            i, j = q.popleft()
            ans[i][j] = v + 1
            for ii, jj in ok[v]:
                q.append((ii, jj))
            continue

        if gx[v] == -1 or gy[v] == -1:
            if not ok[v]:
                print("No")
                return
            i, j = ok[v].pop()
            ans[i][j] = v + 1
            for ii, jj in ok[v]:
                q.append((ii, jj))
            continue

        i, j = gx[v], gy[v]
        ans[i][j] = v + 1
        for ii, jj in ok[v]:
            if i != ii or j != jj:
                q.append((ii, jj))

    print("Yes")
    for r in ans:
        print(*r)

for _ in range(int(input())):
    solve()

posted:
last update: