結果

問題 No.1910 High Element on Grid
ユーザー tktk_snsn
提出日時 2022-04-22 22:54:26
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,581 bytes
コンパイル時間 224 ms
コンパイル使用メモリ 82,516 KB
実行使用メモリ 288,620 KB
最終ジャッジ日時 2024-06-24 04:14:16
合計ジャッジ時間 5,753 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 2 WA * 1 RE * 7 TLE * 1 -- * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque


H, W = map(int, input().split())
R = list(map(int, input().split()))
C = list(map(int, input().split()))


def F(i, j):
    return i * W + j


def parse(id):
    return divmod(id, W)


deg = [0] * (H * W)
G = [[] for _ in range(H * W)]

for i, r in enumerate(R):
    for j in range(r-1):
        G[F(i, j+1)].append(F(i, j))
        deg[F(i, j)] += 1
    for j in range(r-1, W-1):
        G[F(i, j)].append(F(i, j+1))
        deg[F(i, j+1)] += 1

for j, c in enumerate(C):
    for i in range(c-1):
        G[F(i+1, j)].append(F(i, j))
        deg[F(i, j)] += 1
    for i in range(c-1, H-1):
        G[F(i, j)].append(F(i+1, j))
        deg[F(i+1, j)] += 1


def prin():
    for i in range(H):
        for j in range(W):
            print(deg[i*W+j], end=" ")
        print()
    print()


val = H * W + 10
ans = [[-1] * W for _ in range(H)]
cand = list(range(H * W))
que = deque()
for i, d in enumerate(deg):
    if d == 0:
        que.append(i)
while que or cand:
    found = False
    if que:
        while True:
            s = que.popleft()
            x, y = parse(s)
            if ans[x][y] == -1:
                found = True
                break
    else:
        cand.sort(key=lambda x: (-deg[x]))
        while cand:
            s = cand.pop()
            x, y = parse(s)
            if ans[x][y] == -1:
                found = True
                break
    if not found:
        break
    ans[x][y] = val
    val -= 1
    for t in G[s]:
        deg[t] -= 1
        if deg[t] == 0:
            que.append(t)

for a in ans:
    print(*a)
0